Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    An Alien is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Unhappy No idea how to make this!

    UPDATED NEW CODE
    I thought about it some more in a quite place. And since I couldn't get arrays and the other more advanced things to work, I decided to just use the very basics, even if I do have to use a million "if" statements.
    I started all over with only the basics I was taught and came up with new code. But I now ran into a problem. See the comments in the while loop after the print statement.
    Java Code:
    import java.util.Scanner;
    public class TikTakToe {
    
    public static void main(String[] args) {
    
    	Scanner scanner = new Scanner(System.in);
    	String space1 = "1"; //these are just for visual coordinated. These will always be on the screen along with any Xs and Os.
    	String space2 = "2";
    	String space3 = "3";
    	String space4 = "4";
    	String space5 = "5";
    	String space6 = "6";
    	String space7 = "7";
    	String space8 = "8";
    	String space9 = "9";
    	
    	String board1 = "X"; //these actually keep track of the Xs and Os, they are printed along side of the char spaces that I have above. So
    	String board2 = "O"; //for example, the grid will look like this when game is going on
    	String board3 = "X"; //1 X 2 X 3 O
    	String board4 = "O"; //4 O 5 X 6 O
    	String board5 = "X"; //7 O 8 X 9 (as you can see, X has won, and space nine is empty. That is what I'm trying to achieve
    	String board6 = "O";
    	String board7 = "X";
    	String board8 = " ";
    	String board9 = "O";
    	boolean x = false; //I don't know what I am doing with these, but I was thinking maybe 
    	boolean o = false; //to see who wins. 
    	int input = 0;
    	
    	
    	
    	while //loop all this till all spaces are filled. Then to find who wins, I get more specific later.
    	((board1 == " ") || (board2 == " ") || (board3 == " ") || (board4 == " ") || (board5 == " ")
    	|| (board6 == " ") || (board7 == " ") || (board8 == " ") || (board9 == " ")){
    	
    	System.out.println(space1 +"|"+ board1+"\t"+space2 +"|"+ board2+"\t"+space3+"|"+board3);//I want to print out the board here and have it updated each time a user picks a space
    	System.out.println(space4 +"|"+ board4+"\t"+space5 +"|"+ board5+"\t"+space6+"|"+board6);
    	System.out.println(space7 +"|"+ board7+"\t"+space8 +"|"+ board8+"\t"+space9+"|"+board9);
    	//everytime it goes through the loop, it will print the updated board to the screen.
    	//I don't know if I should create a separate method for that (which I have tried, but ran into problems)
    	
    	
    	
    	//check the input in this loop		 
    	do{
    			System.out.print("Enter the number for the space you want to choose: ");
    			input = scanner.nextInt();
    			
    				if (input == 1)
    				board1 = "X";
    				
    				if (input == 2)
    				board2 = "X";
    				
    				if (input == 1)
    				board3 = "X";
    				
    				if (input == 1)
    				board4 = "X";
    				
    				if (input == 1)
    				board5 = "X";
    				
    				if (input == 1)
    				board6 = "X";
    				
    				if (input == 1)
    				board7 = "X";
    				
    				if (input == 1)
    				board8 = "X";
    				
    				if (input == 1)
    				board9 = "X";
    			
    			} while(input != 0 && input < 9);
    		
    		
    		}
    	
    	
    	
    	
    	
    	}
    }


    -----------------------------------------------------------------------------------------------------------------------
    Original Post:
    I'm trying to code a tic tac toe program and I need some serious help. I'm Bucky's java tut #35 so I'm really just a beginner.

    I'm trying to make a simple 1 player (meaning that I need to create instructions for an AI) java program without a GUI.

    I've got this so far and I'm stumped on how to continue.

    Java Code:
    import java.util.Scanner;
    
    
    public class TikTakToe {
    	public static void main(String[] args) {
    		Scanner Scanner = new Scanner(System.in);
    		int[] grid = {0, 0, 0, 0, 0, 0, 0, 0, 0}; //grid, I didn't use multi-dimen. arrays because I don't know how to. 
    		int human = 1; \\change the space into 1 if human picks it
    		int ai = 2; \\same but change to 2
    		
    		
    		System.out.println(grid[0] + "   |   " + grid[1] + "   |   " + grid[2]);
    		System.out.println(grid[3] + "   |   " + grid[4] + "   |   " + grid[5]);
    		System.out.println(grid[6] + "   |   " + grid[7] + "   |   " + grid[8]);
    	
    	}
    
    }
    Would really appreciate it if you guys would work with me building this program.

    I was thinking that I use if statements to decide how the comp. makes moves.

    For example: If human takes middle, then take a corner, if human takes a corner, take side or whatever.

    And then make an if statement for each combination of 3. If grid[0] && grid[1] && grid[2] = 1, then human wins because human is 1.

    What I'm stuck on is that how do I have the player choose his/her spot and then keep track of those spaces and the comp's spaces. I was thinking maybe with coordinated but I don't know how to do it that way.
    Last edited by An Alien; 01-05-2011 at 02:33 PM.

  2. #2
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    You can use a one dimensional array of characters char[] to represent the board, one player is 'X' and the other is 'O'. Each square is numbered from 1 to 9, and so a player would make a move by typing a number n between 1 and 9. If it is not occupied (i.e. the element at index n - 1 was not X or O) then it is marked with either X or O.

    You can add a helper method to determine if a game had been won after each move was made.

    My suggestion is to implement a simple two player game first with checking for a winner. After you have that running, you can begin writing AI component.

    Best,
    --user0--

  3. #3
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Agreed; start with a two human player version.
    Just let the users select positions without checking them, just that they are valid.
    Then move on the checking if they are occupied.
    Next step; in a separate method, check for a winner,
    return 0 for none, 1 for player 1, 2 for player 2, 3 for tied (all position are occupied without a winner).
    Finally make an AI, let the player choose between single- and multiplayer, and then make a GUI.

    I am neutral on the character array part, but if used you should initialise empty position with ' ', if you use int[], you can initialise grid with
    new int[9] instead of {0, 0, 0, 0, 0, 0, 0, 0, 0}.

    If you do not assign the values in an array, all elements will be
    0 for numeric types (0 = '\0' for char), false for boolean and null
    for objects, e.g. String.


    Multidimensional arrays are arrays containing arrays, which means that they are declared by for example int[][] or (if you prefer int name[][] or even int[] name[]). A two dimensional array does not need the be rectangular (the rows do not need to have the same number of columns).
    To make a 3 rows 4 columns matrix (rectangular 2D array) you write
    int[][] matrix = new int[3][4];

    The 3:rd (1-based) row is an array of the length 4 and can be acquired by typing array = matrix[2]. The last column's value on that row can acquired by typing array[3] or matrix[2][3].

    Not that when a multidimensional array is defined, the length of each dimension are declared in the same order as when getting or setting elements, i.e. in reversed order.


    If you use a matrix as 'grid' I would suggest that the coordinates are typed
    Java Code:
    11  21  31
    12  22  32
    13  23  33
    which mean that when a coordinate is typed, you get the two characters an assign them to the variables 'x' and 'y'.
    'char' is numerical (unsigned 16 bit integer), with means you can use arithmetic operations on them.
    So if you subtract 'x' and 'y' with the character '1' you get the position in 'grid'
    by typing grid[y][x]. Of course you can have x before y in 'grid' if you prefer.
    Look into String.charAt.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  4. #4
    An Alien is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    Okay, I'm using the char array following your suggestion, but I didn't get really far.

    Code:
    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    
    public class TikTakToe {
    
    	@SuppressWarnings("unused")
    	public static void main(String[] args) {
    		Scanner Scanner = new Scanner(System.in);
    		int[] coord = {1, 2, 3, 4, 5, 6, 7, 8, 9}; //just for visual coordinates
    		char[] board = {' ',' ',' ',' ',' ',' ',' ',' ',' '}; //actual Xs and Os, array to keep track of spaces.
    		char human = 'X'; //X to fill in the array above when user chooses space
    		char ai = 'O'; //same but O and for AI.
    		
    		System.out.println("Enter the number for the space you want to choose.");
    		char[] board = Scanner.next(); //the board will equal the coord and put a X or an O there. 
    		
    		
    		
    		System.out.println(coord[0] + "   |   " + coord[1] + "   |   " + coord[2]);
    		System.out.println(coord[3] + "   |   " + coord[4] + "   |   " + coord[5]);
    		System.out.println(coord[6] + "   |   " + coord[7] + "   |   " + coord[8]);
    		
    
    		
    		
    		
    		
    	}
    
    }

  5. #5
    An Alien is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    I get an error on this line:
    char[] board = Scanner.next();

  6. #6
    An Alien is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    I changed it to this:
    char[] board = (char[])System.in.read();
    still no work

  7. #7
    An Alien is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    Okay, I've got the scanner to read the char, but I'm trying to think of what to do next.

  8. #8
    An Alien is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    My updated code that does not work:
    import java.io.IOException;
    import java.util.Scanner;
    import java.util.Random;

    public class TikTakToe {

    @SuppressWarnings("unused")
    public static void main(String[] args) throws IOException {
    Scanner Scanner = new Scanner(System.in);
    int[] coord = {1, 2, 3, 4, 5, 6, 7, 8, 9}; //just for visual coordinates
    char[] board = {' ',' ',' ',' ',' ',' ',' ',' ',' '}; //actual Xs and Os, array to keep track of spaces.
    char human = 'X'; //X to fill in the array above when user chooses space
    char ai = 'O'; //same but O and for AI.
    char userinput = 1;

    System.out.println("Enter the number for the space you want to choose.");
    userinput = (char) System.in.read(); //the board will equal the coord and put a X or an O there.
    char[userinput] board = human;

    System.out.println(coord[0] + " | " + coord[1] + " | " + coord[2]);
    System.out.println(coord[3] + " | " + coord[4] + " | " + coord[5]);
    System.out.println(coord[6] + " | " + coord[7] + " | " + coord[8]);




    }

    }

  9. #9
    An Alien is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    I gave up on arrays since I couldn't get them to work for me, now I'm starting all over from just regular variables.

  10. #10
    An Alien is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    Bump, see first post for completely new and updated code. New problem!

  11. #11
    An Alien is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    Bump..............

  12. #12
    An Alien is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    Bumpity Bump

  13. #13
    An Alien is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    Talk about nanotuple posts.

    I feel that nobody is here but me :'(

    I changed all the var to string and I can finally concatenate. Stupid chars weren't working for some reason.
    Code with strings
    Java Code:
    import java.util.Scanner;
    public class TikTakToe {
    
    public static void main(String[] args) {
    
    	Scanner scanner = new Scanner(System.in);
    	String space1 = "1"; //these are just for visual coordinated. These will always be on the screen along with any Xs and Os.
    	String space2 = "2";
    	String space3 = "3";
    	String space4 = "4";
    	String space5 = "5";
    	String space6 = "6";
    	String space7 = "7";
    	String space8 = "8";
    	String space9 = "9";
    	
    	String board1 = "X"; //these actually keep track of the Xs and Os, they are printed along side of the char spaces that I have above. So
    	String board2 = "O"; //for example, the grid will look like this when game is going on
    	String board3 = "X"; //1 X 2 X 3 O
    	String board4 = "O"; //4 O 5 X 6 O
    	String board5 = "X"; //7 O 8 X 9 (as you can see, X has won, and space nine is empty. That is what I'm trying to achieve
    	String board6 = "O";
    	String board7 = "X";
    	String board8 = " ";
    	String board9 = "O";
    	boolean x = false; //I don't know what I am doing with these, but I was thinking maybe 
    	boolean o = false; //to see who wins. 
    	int input = 0;
    	
    	
    	
    	while //loop all this till all spaces are filled. Then to find who wins, I get more specific later.
    	((board1 == " ") || (board2 == " ") || (board3 == " ") || (board4 == " ") || (board5 == " ")
    	|| (board6 == " ") || (board7 == " ") || (board8 == " ") || (board9 == " ")){
    	
    	System.out.println(space1 +"|"+ board1+"\t"+space2 +"|"+ board2+"\t"+space3+"|"+board3);//I want to print out the board here and have it updated each time a user picks a space
    	System.out.println(space4 +"|"+ board4+"\t"+space5 +"|"+ board5+"\t"+space6+"|"+board6);
    	System.out.println(space7 +"|"+ board7+"\t"+space8 +"|"+ board8+"\t"+space9+"|"+board9);
    	//everytime it goes through the loop, it will print the updated board to the screen.
    	//I don't know if I should create a separate method for that (which I have tried, but ran into problems)
    	
    	
    	
    	//check the input in this loop		 
    	do{
    			System.out.print("Enter the number for the space you want to choose: ");
    			input = scanner.nextInt();
    			
    				if (input == 1)
    				board1 = "X";
    				
    				if (input == 2)
    				board2 = "X";
    				
    				if (input == 1)
    				board3 = "X";
    				
    				if (input == 1)
    				board4 = "X";
    				
    				if (input == 1)
    				board5 = "X";
    				
    				if (input == 1)
    				board6 = "X";
    				
    				if (input == 1)
    				board7 = "X";
    				
    				if (input == 1)
    				board8 = "X";
    				
    				if (input == 1)
    				board9 = "X";
    			
    			} while(input != 0 && input < 9);
    		
    		
    		}
    	
    	
    	
    	
    	
    	}
    }

  14. #14
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Always read input with
    line = scanner.nextLine();
    If you want, for example, an integer use Integer.parseInt(line).

    'line' is a String, and the character at a position, i, can be retrieved using
    line.charAt(i).
    'line' can be converted to a character array using
    line.toCharArray().
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  15. #15
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    Quote Originally Posted by An Alien View Post
    Bumpity Bump
    That is one way to guarantee that nobody wants to help you.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  16. #16
    An Alien is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    Thank You Hibernate for helping me. But after waiting so long, I've already gotten my problem solved on a different forum. So this can be locked.

    @Kevin, you can GTFO because someone right above you did help me, so that just makes you look completely stupid saying that and you have just wasted your own time.
    Last edited by An Alien; 01-07-2011 at 08:13 PM.

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by An Alien View Post
    Thank You Hibernate for helping me. But after waiting so long, I've already gotten my problem solved on a different forum. So this can be locked.
    Great, now Hibernate has wasted his time answering a question that has already been answered elsewhere, and how fair is that to him? For this reason we and most all other forums request that you notify all involved forums if you cross-post.

  18. #18
    An Alien is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    I cross posted my questions yesterday and since my thread here was already sinking and how I had not received any comments, I was thinking that it would just die. And on the other forum, I got my answer in just two days. But Hibernate's post and time did not go to waste because I did learn something from it and I believe other beginners like me will also if they ever search up this thread. You can lock this now if you wish.

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

    Default

    Quote Originally Posted by An Alien View Post
    I cross posted my questions yesterday and since my thread here was already sinking and how I had not received any comments, I was thinking that it would just die. And on the other forum, I got my answer in just two days. But Hibernate's post and time did not go to waste because I did learn something from it and I believe other beginners like me will also if they ever search up this thread. You can lock this now if you wish.
    How does your reply in any way argue against posting a simple notification in all cross-posts about the other? It takes but a second to do and is greatly appreciated by all. The converse is also true.

  20. #20
    An Alien is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    Because, I wasn't expecting anyone to post here since nobody had for such a long time. I could have posted, but I decided to let it go. Also, there are multiple ways to solve a problem, I could have 2 or 3 threads asking the same question, having multiple different ways. Which happened here because what Hibernate posted last was not discussed on the other forum. I know I probably should have posted a notification and I am sorry if I wasted anyone's time, but I assure you that Hibernate did not waste his time if was trying to help me because he did.

Page 1 of 2 12 LastLast

Similar Threads

  1. Idea
    By Engineer in forum New To Java
    Replies: 1
    Last Post: 09-19-2010, 07:02 PM
  2. Idea for a program
    By Ejava in forum New To Java
    Replies: 0
    Last Post: 04-11-2010, 06:55 PM
  3. need an Idea ...
    By Mekonom in forum AWT / Swing
    Replies: 43
    Last Post: 11-29-2009, 03:26 PM
  4. I have an idea !!
    By HosHos in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 06:38 AM
  5. i couid not get the idea:
    By sivasayanth in forum New To Java
    Replies: 2
    Last Post: 01-18-2008, 05:52 PM

Tags for this Thread

Posting Permissions

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