Results 1 to 3 of 3
  1. #1
    GoBreak is offline Member
    Join Date
    Jul 2014
    Posts
    2
    Rep Power
    0

    Angry Java Tic Tac Toe Game

    Here is my code, it gives this result and I dont know how to fix it, and i tried everything

    Welcome to tic-tac-toe
    Enter coordinates for your move following the X and O prompts
    Player X Wins!!!!

    here is the code:

    import java.util.Scanner;

    public class TicTacToe{



    private char[][] board;
    private char player; // 'X' or 'O'


    /*
    * Instantiate board to be a 3 by 3 char array of spaces.
    * Set player to be 'X'.
    */
    public TicTacToe() {
    player = 'X';
    board = new char[3][3];
    for (int i = 0; i <= 2; i++) {
    for (int j = 0; j <= 2; j++) {
    board[i][j] = ' ';
    }
    }
    }



    /*
    * If s represents a valid move, add the current player's symbol to the board and return true.
    * Otherwise return false.
    */
    public boolean play(String s) {
    int row = 0, column = 0;
    if(s.toLowerCase().contains("a")) {
    row = 0;
    } if(s.toLowerCase().contains("b")) {
    row = 1;
    }if(s.toLowerCase().contains("c")) {
    row = 2;
    }if(s.toLowerCase().contains("1")) {
    column = 0;
    }if(s.toLowerCase().contains("2")) {
    column = 1;
    }if(s.toLowerCase().contains("3")) {
    column = 2;
    } if(board[row][column] != ' ') {
    board[row][column] = getPlayer();
    return true;
    } else {
    return false;
    }
    }

    /*
    * Switches the current player from X to O, or O to X.
    */
    public void switchTurn() {
    if (player == 'X') {
    player = 'O';
    } else {
    player = 'X';
    }
    }

    /*
    * Returns true if the current player has won the game.
    * Three in a row, column or either diagonal.
    * Otherwise, return false.
    */
    public boolean won() {
    //Rows
    if((board[0][0] == board[0][1]) && (board[0][1] == board[0][2])) {
    return true;
    }if((board[1][0] == board[1][1]) && (board[1][1] == board[1][2])) {
    return true;
    }if((board[2][0] == board[2][1]) && (board[2][1] == board[2][2])) {
    return true;
    //Columns
    }if((board[0][0] == board[1][0]) && (board[1][0] == board[2][0])) {
    return true;
    }if((board[0][1] == board[1][1]) && (board[1][1] == board[2][1])) {
    return true;
    }if((board[0][2] == board[1][2]) && (board[1][2] == board[2][2])) {
    return true;
    }
    //Diagonals
    if((board[0][0] == board[1][1]) && (board[1][1] == board[2][2])) {
    return true;
    }if((board[0][2] == board[1][1]) && (board[1][1] == board[2][0])) {
    return true;
    } else {
    return false;
    }


    }

    /*
    * Returns true if there are no places left to move
    */
    public boolean stalemate() {
    for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
    if (board[i][j] != ' ') {
    return false;
    } else {
    return true;
    }
    }
    }
    return true;
    }
    public char getPlayer() {
    return player;
    }
    public void print() {
    System.out.println();
    System.out.println("\t 1 2 3");
    System.out.println();
    System.out.println("\tA "+board[0][0]+"|"+board[0][1]+"|"+board[0][2]);
    System.out.println("\t -----");
    System.out.println("\tB "+board[1][0]+"|"+board[1][1]+"|"+board[1][2]);
    System.out.println("\t "+"-----");
    System.out.println("\tC "+board[2][0]+"|"+board[2][1]+"|"+board[2][2]);
    System.out.println();
    }


    public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    TicTacToe game = new TicTacToe();
    System.out.println("Welcome to tic-tac-toe");
    System.out.println("Enter coordinates for your move following the X and O prompts");

    while(!game.stalemate()) {
    game.print();
    String move = in.next();
    if(game.play(move)) {
    game.switchTurn();
    } else {
    System.out.println("Enter a valid move");
    break;
    }



    //If the game is won, call break;
    if(game.won()) {
    break;
    }


    }
    if(game.won()){
    System.out.println("Player "+game.getPlayer()+" Wins!!!!");
    } else {
    System.out.println("Stalemate");
    }
    }
    }

  2. #2
    Zwander is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: Java Tic Tac Toe Game

    Please edit this and post it with the code tags and proper indentation. It's just too hard to read this way and you are unlikely to get help.

    Also the problem is this:
    Java Code:
    if((board[0][0] == board[0][1]) && (board[0][1] == board[0][2]))
    board[0][0]==' ' at the start, as does board[0][1] and board [0][2].
    Last edited by Zwander; 07-16-2014 at 07:55 AM.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Java Tic Tac Toe Game

    Also, your stalemate( ... ) method is incorrect; it returns true/false depending on the value of the first cell of the board.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Java Game Doesn't Work With Java 7
    By anthropamorphic in forum Java Gaming
    Replies: 1
    Last Post: 02-15-2013, 06:01 AM
  2. Replies: 4
    Last Post: 07-08-2012, 10:07 AM
  3. Replies: 4
    Last Post: 01-24-2012, 04:51 PM
  4. Replies: 1
    Last Post: 06-02-2011, 10:29 AM

Posting Permissions

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