Results 1 to 10 of 10
  1. #1
    AshtonRose is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default BlueJ Tic-Tac-Toe Board

    So I have a basic code for running a tic-tac-toe board. I'm supposed to fill in the missing pieces, but I've having a lot of problems. I'm supposed to use double nested arrays I believe.
    Requirements:
    As soon as one player gets three in a row, the game should end.
    If the board fills up, the program should print that there was a tie, and the game should end.
    If the user enters a row/column that is outside the board (less than 0 or more than 2), print an error and let them enter another spot. Similarly, if they enter row/column and another piece is already in that spot, print and error and let them enter another position.

    I mostly need some pointers, and any code help would be appreciated. When I've run the program, it just keeps looping through the Player X inputs and won't stop or show the board. What's in bold is what I have added to it.
    Java Code:
    //import java.util.*;
    public class Proj5 {
    	public static void main(String[] args) {
    		Scanner s = new Scanner(System.in);
    		char[][] board = new char[3][3];
    
    		//fill with _ for blank spots
    		for (int i = 0; i < 3; i++) {
    			for (int j = 0; j < 3; j++) {
    				board[i][j] = '_';
    			}
    		}
    
    		//count how many moves have been made
    		int moves = 0;
    
    		//keep track of whose turn it is
    		char turn = 'X';
    
    		//print the initial board
    		System.out.println("Current board: ");
    		for (int i = 0; i < 3; i++) {
    			for (int j = 0; j < 3; j++) {
    				System.out.print(board[i][j] + " ");
    			}
    			System.out.println();
    		}
    		System.out.println();
    
    	//keep playing while less than 9 moves
    		while (moves < 9) {
    	//YOU DO THIS
    	//Ask the user for the row and the column
    
    	//check to see that it is a valid move (row/col should be from 0-2,
    	//and that value in the array should be '_' for empty)
    
    	//If it is valid:
    	//place the user's piece (turn) at that spot in the array
    	//print the board (copy and paste the loops above that printed the initial board)
    
    				//check for a winner
                            [B] int column;
    			int row;
    			System.out.print("Player X, enter row: ");
    			row = Integer.parseInt(s.nextLine());
    			System.out.print("Player X, enter column: ");
    			column = Integer.parseInt(s.nextLine());
    			if (moves == 1 || moves == 3 || moves == 5 || moves == 7 || moves == 9 && 
    row == 0 || row == 1 || row == 2 && column == 0 || column == 1 || column == 2){
    			    
    			 }[/B]
    	[B]if (moves == 2 || moves == 4 || moves == 6 || moves == 8 && 
     row == 0 || row == 1 || row == 2 && column == 0 || column == 1 || column == 2) {
    			     
    			 }[/B]
    				boolean win = false;
    				for (int i = 0; i < 3; i++) {
    					//checks for three in a row
    					if (board[i][0] == board[i][1] &&
    						board[i][1] == board[i][2] &&
    						board[i][0] == turn) win = true;
    
    					//checks for three in a column
    					if (board[0][i] == board[1][i] &&
    						board[1][i] == board[2][i] &&
    						board[0][i] == turn) win = true;
    				}
    
    				//YOU DO THIS
    				//check for three on a diagonal (note that there are two diagonals)
    
    				//Prints who won
    				if (win) {
    					System.out.println(turn + " wins!");
    
    					//end the game
    					break;
    				}
    
    				//YOU DO THIS
    				//switch whose turn it is
    				//if turn is 'X' right now, make it 'O'
    				//otherwise, make it 'X'
    
    				//One more move has gone by
    				moves++;
    		}
    		//if moves made it to 9, must be a tie
    		if (moves == 9) System.out.println("Tie game.");
    }}}//
    Last edited by AshtonRose; 04-09-2011 at 06:30 AM.

  2. #2
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default

    First of all, please edit your post to include code tags around your code. You do it like this:

    Java Code:
    // Your code here
    The tags will make your code so much easier to read, and they will correctly indent everything for readers as well. You'll get much more feedback from the forum if you use code tags on your code.

    Once this is done, I'll start helping you out with this, too :D

  3. #3
    AshtonRose is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default um

    Um I tried that, and I can't get it to work. I'm kinda dumb at this. I don't know how to get those // to work

  4. #4
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default

    To format your code, place it in [code][/code] tags.

    To test for odd/even, instead of using "if (moves == 1 || moves == 3 || ... || moves == 9)", use the modulus operator:

    Java Code:
    if (moves % 2 == 1) {
        //test for player 1 win
    } else {
        //test for player 2 win
    }

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

    Default

    Quote Originally Posted by AshtonRose View Post
    Um I tried that, and I can't get it to work. I'm kinda dumb at this. I don't know how to get those // to work
    You've tried that??? His suggestion has nothing to do with getting your code to work and all to do with helping you make your posted code readable in this forum. I second his suggestion that you edit your post above and use code tags, else we won't be able to read your code and we won't be able to help you.

    To use code tags, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]

  6. #6
    AshtonRose is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default fixed

    I've never posted here, so I didn't know how to do. I wasn't trying to be mean, I just didn't know how to get it do work properly. It's my first time, please just give me a break.

  7. #7
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default

    Oh don't worry about it dude, we're not mad or anything (well, I don't know about Fubar and anyone else, but I'm not :D)
    Anyway, on to your code.
    It continues to repeat input for player X because you only added code that only asks character X what he wants to input. I didn't see a single:
    Java Code:
    System.out.println("Character O Input: Row: ");
    line of code in there. Also, when you add this, make sure to include a few if statements, or just an if-else to test:
    Java Code:
    if(turn == X)
    {
      // blabla Character X code
    }
    else {
      // blabla Character Y code
    }
    Lastly, you never added another for loop nested in another for loop bit of code that displays the current board in your while loop for character input and your game.

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

    Default

    A couple of suggestions:

    • The error message is telling your that turn hasn't been initialized, and so the solution is to initialize turn to something. Note that you currently have it written like so:
      Java Code:
      char turn;
      But you want to initialize it to something that makes sense. Can you guess what it should be? Hint: who has the first turn, x or y?
    • You'll want to change your turn from X to O and from O to X. If you can't use the mod operator recommended above (the "%" operator), then use an if block. So if the most recent turn was x's, (i.e., turn = 'x'), what should the next turn be? Visa versa if the last turn was O's. Can you think of an if block that does this for you? Even if your if block is wrong, give it a try and let's see it.

  9. #9
    AshtonRose is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default Sorry!

    Okay, so my professor FINALLY emailed me, and evidently I did the whole thing wrong, and I changed too much! So...yeah... now I kinda want to delete the whole thread so people don't get confused. I ended up starting from scratch, but I do appreciate you guys helping me, although I'm too dim to get it right.

  10. #10
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default

    Oh don't worry about it man, just mark the thread as "Solved" now that your question is answered. You do that by going to the top of this thread, looking at the options to the right of your first post in this thread, finding "Thread Tools", and picking "Mark This Thread As Solved" at the bottom of the list. Then people will know the problem is solved!

Similar Threads

  1. Help with JAVA board game
    By corky1501 in forum New To Java
    Replies: 5
    Last Post: 02-09-2011, 04:18 AM
  2. TINI Board
    By ApheoXAIO in forum New To Java
    Replies: 1
    Last Post: 07-03-2010, 01:52 PM
  3. How to do a score board?
    By vlan in forum Java Applets
    Replies: 11
    Last Post: 06-03-2010, 11:10 AM
  4. need help making a game board
    By Don k in forum New To Java
    Replies: 2
    Last Post: 04-30-2010, 01:09 AM
  5. game board design
    By tomitu in forum New To Java
    Replies: 12
    Last Post: 02-27-2010, 06:31 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
  •