Results 1 to 12 of 12

Thread: Tictactoe game

  1. #1
    Fowler is offline Member
    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    Default 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.

  2. #2
    Fowler is offline Member
    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    Default

    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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,313
    Blog Entries
    1
    Rep Power
    31

    Default

    Quote Originally Posted by Fowler View Post
    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.
    Edit the first post to add code tags. Place the tag

    [code]

    above your block of code, and the tag:

    [/code]

    below your block of code. Luck!

  4. #4
    Fowler is offline Member
    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    Default

    That helped, thanks Fubarable

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,313
    Blog Entries
    1
    Rep Power
    31

    Default

    Quote Originally Posted by Fowler View Post
    My problem now, is to make it so it isn't possible to make a move where there already is an X or O.
    Check your gameboard[row][col] before committing a move. If it already contains a non-space character, then re-ask for input until correct input has been given. You will need to use a while loop to do this.

  6. #6
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    10

    Default

    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("   +---+---+---+---+---+---+---+---+---+---+");
    }
    The advantage of doing it this way is that less code is required, and it is more extensible.
    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
        }
    }
    in your case, checking if the square is empty will be checking if it contains a space character

    Hope this helps!

  7. #7
    Fowler is offline Member
    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by berkeleybross View Post
    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("   +---+---+---+---+---+---+---+---+---+---+");
    }
    The advantage of doing it this way is that less code is required, and it is more extensible.
    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
        }
    }
    in your case, checking if the square is empty will be checking if it contains a space character

    Hope this helps!
    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.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,313
    Blog Entries
    1
    Rep Power
    31

  9. #9
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    10

    Default

    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. #10
    Fowler is offline Member
    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    Default

    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("   +---+---+---+");
    		
    	}
    	
    }

  11. #11
    Fowler is offline Member
    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Have you read my other recommendation above?
    Sorry, I didn't notice it before now. I have tried what you suggested, but it don't give so much sense to me. I am pretty new to this, so I kind of have to get it as specific as possible to know what people mean when I get help :S

  12. #12
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    10

    Default

    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;
    }
    If you dont understand the code, please ask.

Similar Threads

  1. I need feedback on my TicTacToe game
    By kiregad in forum New To Java
    Replies: 4
    Last Post: 03-21-2010, 11:09 PM
  2. Help with tictactoe
    By ximenaacucr in forum New To Java
    Replies: 3
    Last Post: 09-16-2009, 09:30 PM
  3. TicTacToe
    By Thomas Covington in forum New To Java
    Replies: 1
    Last Post: 04-10-2008, 05:51 AM
  4. TicTacToe Game
    By Ebtihal in forum New To Java
    Replies: 0
    Last Post: 01-09-2008, 12:01 PM
  5. TicTacToe
    By Joe3161 in forum New To Java
    Replies: 4
    Last Post: 11-25-2007, 07:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •