Results 1 to 12 of 12
Thread: Tictactoe game
- 10-31-2010, 04:49 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 6
- Rep Power
- 0
Tictactoe game
Hello
I am currently making a tictactoe game for two players. So far I have made the gameboard work, so both players can write the name before they make their X or O which shows up on the board afterwards.
My problem now, is to make it so it isn't possible to make a move where there already is an X or O.
So far, this is what I have made:
Java Code:/* Date: 2010.10.29 */ import java.io.*; // for File import java.util.*; public class tictactoe { public static char[][] gameboard = new char[3][3]; public static Scanner keyboard = new Scanner(System.in); public static String[] player = {"dummy","dummy"}; public static void main(String[] args) { Arrays.fill(gameboard[0], ' '); Arrays.fill(gameboard[1], ' '); Arrays.fill(gameboard[2], ' '); System.out.println("\nWelcome to the game Tic Tac Toe"); System.out.print("Hello, what's the name of player no 1 ? "); player[0] = keyboard.next(); System.out.print("Hello, what's the name of player no ? "); player[1] = keyboard.next(); drawgameboard(); int player = 0; boolean finished = false; while(!finished) { move(player); drawgameboard(); player = (player + 1) % 2; } } public static void move(int playerno) { int col = 99, row = 99; while ((col == 99) || (row == 99)) { System.out.print("\nHello " + player[playernr] + " make your move. (column-letter and row-number): "); String x = keyboard.next(); String y = keyboard.next(); if (x.equalsIgnoreCase("A")) col = 0; if (x.equalsIgnoreCase("B")) col = 1; if (x.equalsIgnoreCase("C")) col = 2; if (y.equals("1")) row = 0; if (y.equals("2")) row = 1; if (y.equals("3")) row = 2; } char draw = 'O'; if (playernr == 0) draw = 'X'; gameboard[row][col] = draw; } public static void drawgameboard() { System.out.println(); System.out.println(); System.out.println(" A B C "); System.out.println(" +---+---+---+"); System.out.printf(" 1 | %c | %c | %c |\n",gameboard[0][0],gameboard[0][1],gameboard[0][2]); System.out.println(" +---+---+---+"); System.out.printf(" 2 | %c | %c | %c |\n",gameboard[1][0],gameboard[1][1],gameboard[1][2]); System.out.println(" +---+---+---+"); System.out.printf(" 3 | %c | %c | %c |\n",gameboard[2][0],gameboard[2][1],gameboard[2][2]); System.out.println(" +---+---+---+"); } }
Last edited by Fowler; 10-31-2010 at 06:06 PM.
- 10-31-2010, 04:53 PM #2
Member
- Join Date
- Oct 2010
- Posts
- 6
- Rep Power
- 0
Sorry it seems a little mixed up. This is my first post, and I didn't knew how to attach the code, so it would be more clear to look at.
-
- 10-31-2010, 05:05 PM #4
Member
- Join Date
- Oct 2010
- Posts
- 6
- Rep Power
- 0
That helped, thanks Fubarable
-
- 10-31-2010, 05:50 PM #6
Im confused.
Are you making tic tac toe, or battleships?
Also, there is a much better way of making those game boards.
For instance:
Java Code:// spiller 1 System.out.println (); System.out.println(" A B C D E F G H I J "); System.out.println(" +---+---+---+---+---+---+---+---+---+---+"); for (int row = 0; row < 10; row++) { if (row == 9) { System.out.print (" " + (row + 1) + "|"); } else { System.out.print (" " + (row + 1) + " |"); } for (int col = 0; col < 10; col++) { System.out.print (" " + gameboard[row][col] + " |"); } System.out.println (""); System.out.println(" +---+---+---+---+---+---+---+---+---+---+"); }
i.e. if i wanted a game board a 100 squares by a hundred, then all i would need to do is increase "row < 10" and "col < 10" to "row < 100" and "col < 100".
If you dont understand the code, please ask.
Onto your actual question.
To stop someone from putting a piece into a filled square, then all you have to do is check the contents of the square.
Some pseudocode -
Java Code:boolean validsquare = false; while (!validsquare) { ask user for square if square == empty { place piece in square validsquare = true; } else { tell user invalid location } }
Hope this helps!
- 10-31-2010, 06:56 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 6
- Rep Power
- 0
Sorry, it seems like I got it mixed up with the upload of the code. I have changed it with my tictactoe-game, instead of the battleship game I am also working on.
As I understand it, I have to make it so that the code you wrote has to be connected somehow to where the c% is in the board. I have tried to do it in some different ways, I just don't seem to see how to handle this one. I would be very thankful if you could help me with this one.
-
Have you read my other recommendation above?
- 10-31-2010, 07:56 PM #9
you need to change the method public static void move(int playerno)
Before you actually make the move (gameboard[row][col] = draw;) you need to check if that position is empty.
You can insert my pseudo code around the parsing and validation of the input.
- 10-31-2010, 10:29 PM #10
Member
- Join Date
- Oct 2010
- Posts
- 6
- Rep Power
- 0
I don't know if I have understood it terribly wrong, but this is how I have inserted your suggestion in my code:
Java Code:import java.io.*; // for File import java.util.*; public class z { public static char[][] gameboard = new char[3][3]; public static Scanner keyboard = new Scanner(System.in); public static String[] player = {"dummy","dummy"}; public static void main(String[] args) { Arrays.fill(gameboard[0], ' '); Arrays.fill(gameboard[1], ' '); Arrays.fill(gameboard[2], ' '); System.out.println("\nWelcome to the game Tic Tac Toe"); System.out.print("Hello, what's the name of player no 1 ? "); player[0] = keyboard.next(); System.out.print("Hello, what's the name of player no ? "); player[1] = keyboard.next(); drawgameboard(); int player = 0; boolean finished = false; while(!finished) { move(player); drawgameboard(); player = (player + 1) % 2; } } public static void move(int playerno) { int col = 99, row = 99; while ((col == 99) || (row == 99)) boolean validsquare = false; while (!validsquare) { //ask user for square System.out.print("\nHello " + player[playerno] + " make your move. (column-letter and row-number): "); String x = keyboard.next(); String y = keyboard.next(); if x,y == empty { if (x.equalsIgnoreCase("A")) col = 0; if (x.equalsIgnoreCase("B")) col = 1; if (x.equalsIgnoreCase("C")) col = 2; if (y.equals("1")) row = 0; if (y.equals("2")) row = 1; if (y.equals("3")) row = 2; validsquare = true; } else { System.out.println("Position taken - please try again!"); } } char draw = 'O'; if (playerno == 0) draw = 'X'; gameboard[row][col] = draw; } public static void drawgameboard() { System.out.println(); System.out.println(); System.out.println(" A B C "); System.out.println(" +---+---+---+"); System.out.printf(" 1 | %c | %c | %c |\n",gameboard[0][0],gameboard[0][1],gameboard[0][2]); System.out.println(" +---+---+---+"); System.out.printf(" 2 | %c | %c | %c |\n",gameboard[1][0],gameboard[1][1],gameboard[1][2]); System.out.println(" +---+---+---+"); System.out.printf(" 3 | %c | %c | %c |\n",gameboard[2][0],gameboard[2][1],gameboard[2][2]); System.out.println(" +---+---+---+"); } }
- 10-31-2010, 10:38 PM #11
Member
- Join Date
- Oct 2010
- Posts
- 6
- Rep Power
- 0
- 10-31-2010, 10:55 PM #12
I really shouldnt do this, but here is what i was thinking:
Java Code:public static void move(int playerno) { System.out.print("\nHello " + player[playernr] + " make your move. (column-letter and row-number): "); boolean validMove = false; int row = -1; int col = -1; while (!validMove) { // get input String x = keyboard.next(); String y = keyboard.next(); // parse input if (x.equalsIgnoreCase("A")) col = 0; if (x.equalsIgnoreCase("B")) col = 1; if (x.equalsIgnoreCase("C")) col = 2; if (y.equals("1")) row = 0; if (y.equals("2")) row = 1; if (y.equals("3")) row = 2; // check if valid location if (row != -1) { if (col != -1) { // check if square is empty if (gameboard[row][col] == ' ') { validmove = true; } else { System.out.println ("invalid move, try again."); } } else { System.out.println ("Invalid location: Col must be 'A', 'B' or 'C'. Try again."); } } else { System.out.println ("Invalid location: Row must be '1', '2' or '3'. Try again."); } } char draw = 'O'; if (playernr == 0) draw = 'X'; gameboard[row][col] = draw; }
Similar Threads
-
I need feedback on my TicTacToe game
By kiregad in forum New To JavaReplies: 4Last Post: 03-21-2010, 11:09 PM -
Help with tictactoe
By ximenaacucr in forum New To JavaReplies: 3Last Post: 09-16-2009, 09:30 PM -
TicTacToe
By Thomas Covington in forum New To JavaReplies: 1Last Post: 04-10-2008, 05:51 AM -
TicTacToe Game
By Ebtihal in forum New To JavaReplies: 0Last Post: 01-09-2008, 12:01 PM -
TicTacToe
By Joe3161 in forum New To JavaReplies: 4Last Post: 11-25-2007, 07:47 PM
Bookmarks