1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java tic-tac-toe project

  1. Mar 27, 2013 #1
    1. The problem statement, all variables and given/known data
    So, we have a project to make a tic-tac-toe game and for bonus points we can make sure the player(s) can not enter the same position more than once. I can't think of a way to do this. I'm sure I need to use a 'for loop' or a 'while loop'.

    I am using eclipse to write the code. Here is the constructs and methods:
    "
    public class TicTacToe {
    static String P1;
    static String P2;
    static String P3;
    static String P4;
    static String P5;
    static String P6;
    static String P7;
    static String P8;
    static String P9;

    public TicTacToe(){
    P1 = "1";
    P2 = "2";
    P3 = "3";
    P4 = "4";
    P5 = "5";
    P6 = "6";
    P7 = "7";
    P8 = "8";
    P9 = "9";
    }


    public void drawBoard(){
    System.out.println(P1 + "|" + P2 + "|" + P3 + "|");
    System.out.println(P4 + "|" + P5 + "|" + P6 + "|");
    System.out.println(P7 + "|" + P8 + "|" + P9 + "|");
    }
    public boolean checkWin(String player)
    {
    if (P1.equals(player) && P2.equals(player) && P3.equals(player))
    return true;
    else if (P4.equals(player) && P5.equals(player) && P6.equals(player))
    return true;
    else if(P7.equals(player) && P8.equals(player) && P9.equals(player))
    return true;
    else if(P7.equals(player) && P8.equals(player) && P9.equals(player))
    return true;
    else if (P1.equals(player) && P5.equals(player) && P9.equals(player))
    return true;
    else if(P3.equals(player) && P5.equals(player) && P7.equals(player))
    return true;
    else if(P1.equals(player) && P4.equals(player) && P7.equals(player))
    return true;
    else if(P2.equals(player) && P5.equals(player) && P8.equals(player))
    return true;
    else if(P3.equals(player) && P6.equals(player) && P9.equals(player))
    return true;
    else return false;
    }
    public void addX(int Position)
    {
    if(Position == 1)
    P1 = "X";
    else if(Position == 2)
    P2 = "X";
    else if(Position ==3)
    P3 = "X";
    else if(Position == 4)
    P4 = "X";
    else if(Position == 5)
    P5 = "X";
    else if(Position == 6)
    P6 = "X";
    else if(Position == 7)
    P7 = "X";
    else if(Position == 8)
    P8 = "X";
    else if(Position == 9)
    P9 = "X";
    else System.out.println("Please give a number 1-9");
    }
    public void addO(int Position)
    {
    if(Position == 1)
    P1 = "O";
    else if(Position == 2)
    P2 = "O";
    else if(Position ==3)
    P3 = "O";
    else if(Position == 4)
    P4 = "O";
    else if(Position == 5)
    P5 = "O";
    else if(Position == 6)
    P6 = "O";
    else if(Position == 7)
    P7 = "O";
    else if(Position == 8)
    P8 = "O";
    else if(Position == 9)
    P9 = "O";
    else System.out.println("Please give a number 1-9");
    }

    }

    "
    And here is the 'tester' or the part where it runs:
    "import java.util.Scanner;
    public class tictactoetester1 {

    private static Scanner in;

    public static void main(String[] args) {
    TicTacToe game = new TicTacToe();
    boolean gameOver = false;
    in = new Scanner(System.in);
    String turn = "X";
    game.drawBoard();
    for (int i=0; i<9 && !gameOver ; i++)
    {
    if(turn == "X")
    {
    System.out.println("Please type a 1-9, corresponding to the game board");
    in.hasNextDouble();
    int input = (int) in.nextDouble();
    game.addX(input);
    game.checkWin("X");
    game.drawBoard();
    if(game.checkWin("X") == true){
    gameOver = true;
    System.out.println("X Wins!");
    }
    turn = "O";
    }

    else
    if(turn == "O")
    {
    System.out.println("Please type a 1-9, corresponding to the game board");
    in.hasNextDouble();
    int inputO = (int) in.nextDouble(); ;
    game.addO(inputO);
    game.checkWin("O");
    game.drawBoard();
    if(game.checkWin("O") == true){
    gameOver = true;
    System.out.println("O Wins!");
    }
    turn = "X";
    }
    }
    System.out.println("Game Over");

    }
    }
    "
    Any help will be greatly appreciated. Also if something in my code seems really off just tell me. I take criticism well...
    1. The problem statement, all variables and given/known data



    2. Relevant equations



    3. The attempt at a solution
     
  2. jcsd
  3. Mar 27, 2013 #2

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    Just check the value of the position the player used?

    A general remark: If you have to copy code lines more than once, you are doing something wrong.
    Why do you use 9 different strings, with 9 different names? This is a perfect example for an array.

    Where is the point in "public void addX(int Position)" and "public void addO(int Position)", if "public void addO(int Position, String Player)" can do the same?
    The fact that you have 9 nearly identical checks shows the disadvantage of 9 different strings. With an array, this function needs just 1-2 lines.

    Concerning functionality: If you enter a wrong value, it is ignored (just gives an error message) and the other player is allowed to continue, Is this intended?
     
  4. Mar 27, 2013 #3
    Well I don't know how to or what an array is, but I guess I can look it up. Thanks! :) I'll fix the wrong value thing to loop until the correct value is given. Thank you again :)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted