Results 1 to 10 of 10
Thread: 2-dimensional tic-tac-toe
- 02-22-2011, 12:23 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
2-dimensional tic-tac-toe
Hello....This program should detect when a player has won, or when the game ends in a draw. I'm having trouble with my method main part. Here is my program...Can anyone help me solve this please...
Java Code:package tictactoe; import java.util.Scanner; public class Main { static byte[][] board; public boolean xTurn; public Scanner keyboard; public static void main(String[] args) { TicTacToe game = new TicTacToe();//1st problem game.play(); } public static void TicTacToe() { board = new byte[3][3]; for (int r = 0; r < 3; r++) { for (int c = 0; c < 3; c++) { board[r][c] = ' '; } } boolean xTurn = true; keyboard = new Scanner(System.in);//2nd problem } public void displayRow(int row) { System.out.println(" " + board[row][0] + " | " + board[row][1] + " | " + board[row][2]); } public void displayBoard() { displayRow(0); System.out.println("-----------"); displayRow(1); System.out.println("-----------"); displayRow(2); } public void displayMenu() { if (xTurn) { System.out.println("X's Turn!"); } else { System.out.println("O's Turn!"); } System.out.println("What would you like to do?"); System.out.println("1: Make a move"); System.out.println("2: Start Over"); System.out.println("3: Quit"); System.out.print("Choice: "); } public boolean getMove() { boolean invalid = true; int row = 0, column = 0; while (invalid) { System.out.println("Which row, column would you like to move to?" + " Enter two numbers between 0-2 separated by a space to indicate position."); row = keyboard.nextInt(); column = keyboard.nextInt(); if (row >= 0 && row <= 2 && column >= 0 && column <= 2) { if (board[row][column] != ' ') { System.out.println("That position is already taken"); } else { invalid = false; } } else { System.out.println("Invalid position"); } } if (xTurn) { board[row][column] = 'X'; } else { board[row][column] = 'O'; } return winner(row, column); } public void restart() { for (int r = 0; r < 3; r++) { for (int c = 0; c < 3; c++) { board[r][c] = ' '; } } xTurn = true; } public boolean winner(int lastR, int lastC) { boolean winner = false; byte symbol = board[lastR][lastC]; int numFound = 0; for (int c = 0; c < 3; c++) { if (board[lastR][c] == symbol) { numFound++; } } if (numFound == 3) { winner = true; } numFound = 0; for (int r = 0; r < 3; r++) { if (board[r][lastC] == symbol) { numFound++; } } if (numFound == 3) { winner = true; } numFound = 0; for (int i = 0; i < 3; i++) { if (board[i][i] == symbol) { numFound++; } } if (numFound == 3) { winner = true; } numFound = 0; for (int i = 0; i < 3; i++) { if (board[i][2 - i] == symbol) { numFound++; } } if (numFound == 3) { winner = true; } return winner; } public boolean boardFull() { int numSpotsFilled = 0; for (int r = 0; r < 3; r++) { for (int c = 0; c < 3; c++) { if (board[r][c] == 'X' || board[r][c] == 'O') { numSpotsFilled++; } } } return numSpotsFilled == 9; } public void play() { while (true) { displayBoard(); displayMenu(); int choice = keyboard.nextInt(); if (choice == 1) { if (getMove()) { displayBoard(); if (xTurn) { System.out.println("X Wins!"); } else { System.out.println("O Wins!"); } System.exit(0); } else if (boardFull()) { displayBoard(); System.out.println("Draw!"); System.exit(0); } else { xTurn = !xTurn; } } else if (choice == 2) { restart(); } else if (choice == 3) { System.exit(0); } else { System.out.println("Invalid Option"); } } } }
- 02-22-2011, 12:28 AM #2
Do not just dump your code on us and expect us to fix it for you. Provide details and ask specific question.
You have placed comments sucha as "1st problem" in your code. We don't read minds. Tell us exactly what the problem is. Do you get a compiler error? Then copy and paste it so we can see it. Does it run but have incorrect behaviour? Then describe what it is doing and what it should do instead.
- 02-22-2011, 12:39 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
Sorry.....
I'm new to this and I did not mean to drop and run...I thought that it was clear that my problem laid with these two arguments...when I ran this program without the two I mentioned I got no errors and this message " BUILD SUCCESSFUL (total time: 0 seconds)"... but nothing happened...I've tried lots of different methods and args but none help...I don't know if I should remove it or leave it in and if so what else am I missing...once again sorry I am new at this...
- 02-22-2011, 01:40 AM #4
Huh?
I still haven't a clue as to what your problem is.
- 02-22-2011, 01:49 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
Ttt
The problem is that this program wont execute with or without:
public static void main(String[] args) {
TicTacToe game = new TicTacToe();//when removed
game.play();//when removed
}
public static void TicTacToe() {
board = new byte[3][3];
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
board[r][c] = ' ';
}
}
boolean xTurn = true;
keyboard = new Scanner(System.in);//when removed
}
//when these 3 args are removed I get the program to run but all it does and says is "BUILD SUCCESSFUL (total time: 0 seconds)"...and that's all... I'm trying to get it to execute the program ...What are my options???
- 02-22-2011, 01:51 AM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
What are the error messages when you leave that code in there. Post them here.
- 02-22-2011, 01:55 AM #7
Of course it compiles and runs but does nothing if you remove code from the main method.
There is nothing inside the main method so what is it supposed to do? Nothing.Java Code:class Foo { public static void main(String[] args) { } }
Now if you place one or more of those lines back into your code and it does not compile then TELL US WHAT THE ERROR IS AND WHICH LINE CAUSES IT. You may be new to programming but this has nothing to do with programming and everything to do with communication.
- 02-22-2011, 02:02 AM #8
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
//
run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: class TicTacToe
location: class tictactoe.Main
at tictactoe.Main.main(Main.java:12)
Java Result: 1
BUILD SUCCESSFUL (total time: 9 seconds)
//
also: non static variable keyboard cannot be referenced from a static context
- 02-22-2011, 02:08 AM #9
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
In your opinion does this code
call or invoke this methodJava Code:TicTacToe game = new TicTacToe();//1st problem
Is there a class in existence called TicTacToe?Java Code:public static void TicTacToe() { board = new byte[3][3]; for (int r = 0; r < 3; r++) { for (int c = 0; c < 3; c++) { board[r][c] = ' '; } } boolean xTurn = true; keyboard = new Scanner(System.in);//2nd problem }
Problem 2:
Scanner instance keyboard is non static and is being used in a static method
- 02-22-2011, 02:10 AM #10
Similar Threads
-
2 dimensional array help!
By ber1023 in forum New To JavaReplies: 9Last Post: 01-02-2011, 12:29 AM -
Two dimensional array
By niu_niu in forum New To JavaReplies: 4Last Post: 06-13-2010, 12:34 AM -
about two dimensional array
By matin1234 in forum New To JavaReplies: 2Last Post: 06-01-2010, 11:09 AM -
two-dimensional array
By kHim in forum New To JavaReplies: 4Last Post: 11-16-2008, 07:21 PM -
How to reverse two dimensional
By masaka in forum New To JavaReplies: 4Last Post: 05-19-2008, 10:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks