Results 1 to 3 of 3
Thread: Help with game algorithm
- 02-25-2010, 06:19 AM #1
Member
- Join Date
- Dec 2008
- Posts
- 41
- Rep Power
- 0
Help with game algorithm
Im designing a GUI that is supposed to mimic the game connect four, which i have to design an AI for. I have a working build of the board but im having a hard time coming up with a good way to check for a winner without just brute forcing it with loops. Any help greatly appreciated.
The for loop in the gameScore() calculates a win horizontally.Java Code:package connectfour; import java.awt.*; import java.awt.event.*; /** *Tim McColgan * */ public class Board extends Frame { final int row = 6; final int col = 7; int player = 1; int score = 0; final Panel game = new Panel(); Button[][] buttons = new Button[row][col]; Button newGame = new Button("RESTART"); int[][] points = new int[row][col]; Board() { super("Connect Four"); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setSize(500, 500); setVisible(true); setLayout(new BorderLayout()); game.setLayout(new GridLayout(row, col)); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { buttons[i][j] = new Button(); buttons[i][j].setBackground(Color.GRAY); buttons[i][j].addActionListener(new ButtonListener()); points[i][j] = 0; game.add(buttons[i][j]); }//end j }//end i add(game, BorderLayout.CENTER); newGame.addActionListener(new ButtonListener()); add(newGame, BorderLayout.NORTH); }//end Board() private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { boolean restart = false; if (e.getSource() == newGame) { newGame(); restart = true; player = 1 - player; } player = 1 - player; int y = 0; int x = 0; if (!restart) { while (buttons[x][y] != e.getSource()) { x++; if (y == col) { y = 0; } if (x == row) { x = 0; y++; }//end if }//end while while (x < row - 1 && buttons[x + 1][y].getBackground() != Color.red && buttons[x + 1][y].getBackground() != Color.blue && !restart) { if (player == 0) { buttons[x][y].setBackground(Color.BLUE); } else { buttons[x][y].setBackground(Color.RED); }//end elseif try { Thread.sleep(75);//Animate piece drop. } catch (Exception exc) { System.out.println("!"); }//end try-catch if (buttons[x][y].getBackground() != null) { buttons[x][y].setBackground(Color.GRAY); x++; }// end if }//end while if (player == 0) { buttons[x][y].setBackground(Color.blue); points[x][y] = -1; } else { buttons[x][y].setBackground(Color.red); points[x][y] = 1; }//end elseif buttons[x][y].setEnabled(false); } score = gameScore(); if ((score <= 4 && score >= 1) || (score >= -4 && score <= -1)) { System.out.println("Winz"); } }//end if }//end actionPerformed() public int gameScore() { int status = 0; for (int i = row - 1; i > 1; i--) { for (int j = col - 1; j > 1; j--) { status += points[i][j]; System.out.println(i+" "+j + "at point: "+points[i][j]); } if ((status <= 4 && status >= 1) || (status>= -4 && status <= -1)) { return status; } status = 0; } return status; } public void newGame() { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { buttons[i][j].setBackground(Color.GRAY);//reset buttons buttons[i][j].setEnabled(true); //turn on buttons }//end j }//end i }//end newGame() }//end inner class //}//end outer class
- 02-26-2010, 04:24 AM #2
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
There's nothing wrong with brute force when it makes sense to use it, but if you want an algorithm that finds line segments, consider the Hough transform. Treat the grid as if it were an image.
- 02-26-2010, 10:41 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,412
- Blog Entries
- 7
- Rep Power
- 17
You can reduce the search space tremendously by applying a bit of reasoning: before the move was made there was no winner so the position of the last move must be part of the winning configuration. Search, starting from that last position in all eight (*) directions and count the number of coins that have the same colour as the current coin that are adjacent; if you find four or more that last move was a winning move and the game ends.
kind regards,
Jos
(*) actually there are seven directions because the current coin has to be the topmost coin so you don't need to search upwards.Last edited by JosAH; 02-26-2010 at 05:09 PM.
Similar Threads
-
Implementing "Game Over" in Minesweeper game based on Gridworld framework.
By JFlash in forum New To JavaReplies: 2Last Post: 08-05-2010, 04:49 AM -
Help with an Algorithm
By Manfizy in forum New To JavaReplies: 22Last Post: 07-03-2009, 07:16 AM -
O(log n) algorithm help !!!!!!
By itseeker87 in forum New To JavaReplies: 8Last Post: 09-09-2008, 05:12 PM -
Alpha-beta algorithm, game tree
By ljaf_1985 in forum New To JavaReplies: 0Last Post: 03-28-2008, 04:21 PM -
Help with algorithm
By susan in forum New To JavaReplies: 1Last Post: 07-13-2007, 10:26 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks