Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Need some help with a tick tack toe game

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need some help with a tick tack toe game

    I know this is really simple. I have a tick tack toe game and when i run it it works and there are no errors. but the X's and O'x do not show in the board can someone please look at the code and show me the right way to do it. i know this is kinda lame question but i have been looking all over the net and cant seem to figure out the proper way to do it. I know the problem is in the "gameBoard" method and its cause i am telling the code to print the same board every time but i dont know how to do it the right way


    package chap7MDA;

    import java.util.Scanner;


    public class chapexe7we {


    public static void main(String[] args) {

    char[][] board = new char [3][3];//make a game board

    gameBoard(board);// call the method game board to make the board

    do {
    makeAMove(board, 'X');
    gameBoard(board);

    if(wiNNing('X', board)) {
    System.out.println("X player won");
    System.exit(1);
    }

    else if(tie(board)) {
    System.out.println("No body won, It's a Draw Partners");
    System.exit(2);
    }



    makeAMove(board, 'O');
    gameBoard(board);


    if(wiNNing('O', board)) {
    System.out.println("O player won");
    System.exit(3);
    }

    else if(tie(board)) {
    System.out.println("No body won, It's a Draw Partners");
    System.exit(4);
    }

    } while(true);


    }//end main




    public static void gameBoard(char [][] gBoard){

    for(int i = 0; i < 3; i++){//populate the board with boarders
    for(int j = 0; j < i; j++) {
    System.out.println("\n-------------");
    System.out.print("| | | | ");

    }
    }
    System.out.println("\n-------------");//put a line at the bottom

    }//end gameBoard


    public static void makeAMove (char [][]board, char XsAnd0s){
    Scanner input = new Scanner(System.in);//make a scanner object called input

    boolean turn = false;

    do{

    System.out.println("Please enter the row for " + XsAnd0s + ":" );//prompt user
    int row = input.nextInt();//take in a value for the row
    System.out.println("Please enter the column for " + XsAnd0s + ":");//prompt user
    int col = input.nextInt();//take in a value for the column

    if (board[row][col] == 0){
    board[row][col] = XsAnd0s;
    turn = true;
    }
    else {
    System.out.println("The other player already has this spot");
    }

    }
    while(!turn);
    }

    public static boolean wiNNing(char XOs, char[][]board ){

    for (int i = 0; i <3; i++)
    if(XOs == board[i][0] && XOs == board[i][1] && XOs ==board[i][2])
    return true;

    for (int j = 0; j < 3; j++)
    if (XOs == board[0][j] && XOs == board[1][j] && XOs == board[2][j])
    return true;
    if(XOs == board[0][0] && XOs == board[1][1] && XOs == board[2][2])
    return true;

    return XOs == board[0][2] && XOs == board[1][1] && XOs == board[2][0];

    }

    public static boolean tie (char[][] board){
    boolean noWinner = false;
    for(int i = 0; i < 3; i++){
    for(int j = 0; j < 3; j++)
    if(board[i][j] == 0)
    return noWinner;


    }
    return true;
    }



    }//end exercise7


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need some help with a tick tack toe game

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need some help with a tick tack toe game

    Please edit your post and wrap your code with code tags:
    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    to get highlighting and preserve formatting.
    [/code]
    to get highlighting and preserve formatting.

    --- Update ---

    Please edit your post and wrap your code with code tags:
    package chap7MDA;
     
    import java.util.Scanner;
     
     
    public class chapexe7we {
     
     
    public static void main(String[] args) {
     
    char[][] board = new char [3][3];//make a game board
     
    gameBoard(board);// call the method game board to make the board
     
    do {
    makeAMove(board, 'X');
    gameBoard(board);
     
    if(wiNNing('X', board)) {
    System.out.println("X player won");
    System.exit(1);
    }
     
    else if(tie(board)) {
    System.out.println("No body won, It's a Draw Partners");
    System.exit(2);
    }
     
     
     
    makeAMove(board, 'O');
    gameBoard(board);
     
     
    if(wiNNing('O', board)) {
    System.out.println("O player won");
    System.exit(3);
    }
     
    else if(tie(board)) {
    System.out.println("No body won, It's a Draw Partners");
    System.exit(4);
    }
     
    } while(true);
     
     
    }//end main
     
     
     
     
    public static void gameBoard(char [][] gBoard){
     
    for(int i = 0; i < 3; i++){//populate the board with boarders
    for(int j = 0; j < i; j++) {
    System.out.println("\n-------------");
    System.out.print("| | | | ");
     
    }
    }
    System.out.println("\n-------------");//put a line at the bottom
     
    }//end gameBoard
     
     
    public static void makeAMove (char [][]board, char XsAnd0s){
    Scanner input = new Scanner(System.in);//make a scanner object called input
     
    boolean turn = false;
     
    do{
     
    System.out.println("Please enter the row for " + XsAnd0s + ":" );//prompt user
    int row = input.nextInt();//take in a value for the row
    System.out.println("Please enter the column for " + XsAnd0s + ":");//prompt user
    int col = input.nextInt();//take in a value for the column
     
    if (board[row][col] == 0){
    board[row][col] = XsAnd0s;
    turn = true;
    }
    else {
    System.out.println("The other player already has this spot");
    }
     
    }
    while(!turn);
    }
     
    public static boolean wiNNing(char XOs, char[][]board ){
     
    for (int i = 0; i <3; i++)
    if(XOs == board[i][0] && XOs == board[i][1] && XOs ==board[i][2])
    return true;
     
    for (int j = 0; j < 3; j++)
    if (XOs == board[0][j] && XOs == board[1][j] && XOs == board[2][j])
    return true;
    if(XOs == board[0][0] && XOs == board[1][1] && XOs == board[2][2])
    return true;
     
    return XOs == board[0][2] && XOs == board[1][1] && XOs == board[2][0];
     
    }
     
    public static boolean tie (char[][] board){
    boolean noWinner = false;
    for(int i = 0; i < 3; i++){
    for(int j = 0; j < 3; j++)
    if(board[i][j] == 0)
    return noWinner;
     
     
    }
    return true;
    }
     
     
     
    }//end exercise7
    to get highlighting and preserve formatting.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Need some help with a tick tack toe game

    When will this if statement be true?

    if (board[row][col] == 0)

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need some help with a tick tack toe game

    The code has lost all of its indentations?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Need some help with a tick tack toe game

    Well when you print out your game board you never actually check what is inside your board and print it out

Similar Threads

  1. Need help with Logic of a Tic Tac Toe game.
    By Rain_Maker in forum What's Wrong With My Code?
    Replies: 10
    Last Post: April 8th, 2013, 08:14 PM
  2. Need help with tic tac toe game
    By ogpg2006 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 2nd, 2012, 07:34 AM
  3. tic tac toe game problem with X and O
    By woohooXX in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 20th, 2011, 07:34 AM
  4. TIC-TAC-TOE GAME
    By umerahmad in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 29th, 2011, 12:15 PM
  5. tick tack toe hard times!!
    By JpJ in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 12th, 2010, 08:15 AM