Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Java in General
Help with method
John Daniels
Greenhorn
Posts: 4
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I wrote a simple program for tictactoe and have everything working except a method to check and see if a player has won the game diagonally. I am not sure how to write the for loop to check the board.
public class Board extends ReactiveDrawingPanel implements GameConstants { private BoardState _state; private Game _game; private java.util.HashMap<Position, Mark> _boardMap; public Board(Container container, Game game) { super(container); _game = game; this.setColor(java.awt.Color.BLUE); this.setDimension(new java.awt.Dimension(NUM_COLS * SQUARE_SIZE, NUM_ROWS * SQUARE_SIZE)); _state = new NullBoardState(); _boardMap = new java.util.HashMap<Position, Mark>(); } public void start() { this.removeAllGraphics(); this.drawGrid(); _state = new PlayBoardState(); _boardMap.clear(); } public void react(java.awt.Point p) { _state.reactToClick(this, p); } public void placeMark(java.awt.Point p) { //place the mark of the appropriate player Player currentPlayer = _game.getCurrentPlayer(); //which row/column did the user click on? Position pos = new Position(p); if(_boardMap.containsKey(pos)) { //User tried to put X/O in a spot where there was an X/O _game.userError(); } else { //User tried to put X/O in free spot. Mark m = new Mark(this, currentPlayer.getImageName(), currentPlayer); m.setCenterLocation(pos.getCenterLocation()); _boardMap.put(pos, m); _game.playerMoved(); } } public void pause() { System.out.println("board's pause"); _state = new NullBoardState(); for(Mark mark: _boardMap.values()) { mark.hide(); } } public void unpause() { System.out.println("board's unpause"); _state = new PlayBoardState(); for(Mark mark: _boardMap.values()) { mark.show(); } } public void changeToNullState() { _state = new NullBoardState(); } public boolean playerWin(Player currentPlayer){ //check to see if player won in rows for(int row = 0; row < NUM_ROWS; row++) { if (this.rowWin(row, currentPlayer)) { return true; } } //check to see if player won in columns for(int column = 0; column < NUM_COLS; column++) { if (this.columnWin(column, currentPlayer)) { return true; } } //check to see if player won in diagonals [B]if (this.diagonalWin(currentPlayer)) { return true; }[/B] return false; } private boolean rowWin(int row, Player currentPlayer) { for(int column = 0; column < NUM_COLS; column++) { if(!_boardMap.containsKey(new Position(row, column))){ return false; } if ( currentPlayer.getName() != _boardMap.get(new Position(row, column)).getPlayer().getName()) { return false; } } return true; } private boolean columnWin(int column, Player currentPlayer) { for(int row = 0; row < NUM_ROWS; row++) { if(!_boardMap.containsKey(new Position(row, column))){ return false; } if ( currentPlayer.getName() != _boardMap.get(new Position(row, column)).getPlayer().getName()) { return false; } } return true; } [B]private boolean diagonalWin(Player currentPlayer) { return false; }[/B] private void drawGrid() { NGP.Graphics.Line vline, hline; for ( int i = 1; i < 3; i++ ) { vline = new NGP.Graphics.Line(this); vline.setColor(java.awt.Color.WHITE); vline.setPoints(new java.awt.Point(i * SQUARE_SIZE, 0), new java.awt.Point(i * SQUARE_SIZE, 3 * SQUARE_SIZE)); hline = new NGP.Graphics.Line(this); hline.setColor(java.awt.Color.WHITE); hline.setPoints(new java.awt.Point(0, i * SQUARE_SIZE), new java.awt.Point(3 * SQUARE_SIZE, i * SQUARE_SIZE)); } // end of for () } public int getBoardSize() { return _boardMap.size(); } }
Could someone please help.
Trevor Cole
Greenhorn
Posts: 5
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
John,
I wrote a TicTacToe in J2ME, I simply use an if statement to check the value of the three cells in the diagonal...
My code looks like this...
// check first diagonal
if ((cellValues[0] == cellValues[4]) && (cellValues[4] == cellValues[8])) {
tileType = cellValues[0];
}
// check second diagonal
if ((cellValues[2] == cellValues[4]) && (cellValues[4] == cellValues[6])) {
tileType = cellValues[2];
}
cellValues[] holds the current tileType (X, O or null).
Hope this helps,
Trevor
John Daniels
Greenhorn
Posts: 4
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks
Alas, poor Yorick, he knew this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Tic Tac Toe is this good solution
Need some help, pleeaaase!!
app doesn't exit because of JOptionPane
Tile constraints
synchronization.. and threads !!!
More...