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

  • Context: Comp Sci 
  • Thread starter Thread starter Albeaver89
  • Start date Start date
  • Tags Tags
    Java Project
Click For Summary
SUMMARY

The forum discussion focuses on creating a Tic-Tac-Toe game in Java with unique position restrictions using Eclipse. The current implementation uses nine separate string variables for board positions, which is inefficient. Suggestions include using an array to manage positions and implementing input validation to prevent players from selecting already occupied spaces. The discussion emphasizes the need for a loop to ensure valid input and improve code efficiency.

PREREQUISITES
  • Java programming fundamentals
  • Understanding of arrays in Java
  • Basic control structures: loops and conditionals
  • Experience with Eclipse IDE
NEXT STEPS
  • Learn how to implement arrays in Java for managing game states
  • Research input validation techniques in Java to handle user input effectively
  • Explore object-oriented programming principles to refactor the Tic-Tac-Toe code
  • Investigate Java's Scanner class for improved user interaction
USEFUL FOR

Java developers, game developers, and students learning programming concepts who want to enhance their understanding of game logic and user input handling.

Albeaver89
Messages
18
Reaction score
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
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?
 
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 :)
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 11 ·
Replies
11
Views
10K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K