Java Tic-Tac-Toe Project: Create a Game with Unique Position Restrictions

  • Comp Sci
  • Thread starter Albeaver89
  • Start date
  • Tags
    Java Project
In summary, the conversation is about creating a tic-tac-toe game with a bonus point for preventing players from entering the same position more than once. The code provided includes a TicTacToe class with methods for drawing the board, checking for a win, and adding X or O at a specific position. The tester class runs the game and allows for user input. Suggestions are made for improving the code, such as using an array instead of multiple strings and refining the functionality for incorrect user input.
  • #1
Albeaver89
18
0

Homework Statement


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...
 
Physics news on Phys.org
  • #2
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.
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?
 
  • #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 :)
 

1. What is the purpose of the Java tic-tac-toe project?

The purpose of the Java tic-tac-toe project is to create a computer program that allows two players to play the classic game of tic-tac-toe. It is meant to demonstrate the use of programming concepts and techniques, such as object-oriented programming and user input handling.

2. What are the key features of the Java tic-tac-toe project?

The key features of the Java tic-tac-toe project include the ability to display the game board, handle user input, keep track of game progress, and determine a winner or a tie. It also includes error handling to prevent invalid moves and a user-friendly interface.

3. Is the Java tic-tac-toe project suitable for beginners?

Yes, the Java tic-tac-toe project is suitable for beginners who have a basic understanding of Java programming. It is a simple and straightforward project that can help beginners practice their programming skills and understand important concepts.

4. Can the Java tic-tac-toe project be expanded upon or modified?

Yes, the Java tic-tac-toe project can be expanded upon or modified according to the programmer's preferences. Additional features such as a computer player, different game modes, or a graphical user interface can be implemented to make the game more interesting and challenging.

5. Are there any resources available for learning the Java tic-tac-toe project?

There are many online resources and tutorials available for learning the Java tic-tac-toe project. These include step-by-step guides, video tutorials, and code examples that can help beginners understand and create their own version of the game. Additionally, there are also forums and communities where programmers can ask for help and share their projects.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
816
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
461
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
9K
Back
Top