Reputation: 1
Its the final summative for school (yes this is final project worthy because my coding class got shafted last year because of Covid) and I stay away from loops as much as possible, I wanna know how to make this say that player one and two have won. This is in Console HSA btw so there are pretty large parameters on my abilities also the fact that I suck at coding is holding me back. I more did this class for leisure because I enjoyed the little coding we got to do online last year. We used Chromebook's to code and you can probably imagine how awful that was
int startGame;
c.println ("Welcome to Tic Tac Toe But With [] & O!");
c.println ("Each sqaure in the 9X9 grid is just labelled by its placement");
c.println ("So you would type 5 if you want in the first square and so on...");
c.println ("This is Player V. Player so Find a Friend!");
c.println ("________________________________________________________________");
c.println ("Press 1 to start the game");
startGame = c.readInt();
if(startGame == 1){
c.clear();
playOne();
playTwo();
playOne();
playTwo();
playOne();
playTwo();
playOne();
playTwo();
playOne();
}
else{
c.println("Goodbye!");
}
}
public static void playTwo()
{
int playerTwo;
c.println ("Player Two, Where would you like to put your [] ?");
playerTwo = c.readInt ();
c.fillRect (05, 510, 287, 5);
c.fillRect (05, 605, 287, 5);
c.fillRect (195, 420, 5, 287);
c.fillRect (95, 420, 5, 287);
if (playerTwo == 1)
{
c.fillRect (26, 440, 50, 50);
}
if (playerTwo == 2)
{
c.fillRect (124, 440, 50, 50);
}
if (playerTwo == 3)
{
c.fillRect (220, 440, 50, 50);
}
if (playerTwo == 4)
{
c.fillRect (26, 535, 50, 50);
}
if (playerTwo == 5)
{
c.fillRect (124, 535, 50, 50);
}
if (playerTwo == 6)
{
c.fillRect (220, 535, 50, 50);
}
if (playerTwo == 7)
{
c.fillRect (26, 632, 50, 50);
}
if (playerTwo == 8)
{
c.fillRect (124, 632, 50, 50);
}
if (playerTwo == 9)
{
c.fillRect (220, 632, 50, 50);
}
/*if(playerTwo == 1 && playerTwo == 2 && playerTwo == 3)
{
c.clear();
c.println("Player Two Won!");
}
if(playerTwo == 1 && playerTwo == 4 && playerTwo == 7);
{
c.clear();
c.println("Player Two Won!");
}
if(playerTwo == 1 && playerTwo == 5 && playerTwo == 9);
{
c.clear();
c.println("Player Two Won!");
}
if(playerTwo == 2 && playerTwo == 5 && playerTwo == 8);
{
c.clear();
c.println("Player Two Won!");
}
if(playerTwo == 3 && playerTwo == 5 && playerTwo == 7);
{
c.clear();
c.println("Player Two Won!");
}
if(playerTwo == 3 && playerTwo == 6 && playerTwo == 9);
{
c.clear();
c.println("Player Two Won!");
}
if(playerTwo == 4 && playerTwo == 5 && playerTwo == 6);
{
c.clear();
c.println("Player Two Won!");
}
if(playerTwo == 7 && playerTwo == 8 && playerTwo == 9);
{
c.clear();
c.println("Player Two Won!");
}*/
}
public static void playOne()
{
int playerOne;
c.println ("Player 1, Where would you like to put your O ?");
playerOne = c.readInt ();
c.fillRect (05, 510, 287, 5);
c.fillRect (05, 605, 287, 5);
c.fillRect (195, 420, 5, 287);
c.fillRect (95, 420, 5, 287);
if (playerOne == 1)
{
c.fillOval (26, 440, 50, 50);
}
if (playerOne == 2)
{
c.fillOval (124, 440, 50, 50);
}
if (playerOne == 3)
{
c.fillOval (220, 440, 50, 50);
}
if (playerOne == 4)
{
c.fillOval (26, 535, 50, 50);
}
if (playerOne == 5)
{
c.fillOval (124, 535, 50, 50);
}
if (playerOne == 6)
{
c.fillOval (220, 535, 50, 50);
}
if (playerOne == 7)
{
c.fillOval (26, 632, 50, 50);
}
if (playerOne == 8)
{
c.fillOval (124, 632, 50, 50);
}
if (playerOne == 9)
{
c.fillOval (220, 632, 50, 50);
}
/*if(playerOne == 1 && playerOne == 2 && playerOne == 3)
{
c.clear();
c.println("Player One Won!");
}
if(playerOne == 1 && playerOne == 4 && playerOne == 7);
{
c.clear();
c.println("Player One Won!");
}
if(playerOne == 1 && playerOne == 5 && playerOne == 9);
{
c.clear();
c.println("Player One Won!");
}
if(playerOne == 2 && playerOne == 5 && playerOne == 8);
{
c.clear();
c.println("Player One Won!");
}
if(playerOne == 3 && playerOne == 5 && playerOne == 7);
{
c.clear();
c.println("Player One Won!");
}
if(playerOne == 3 && playerOne == 6 && playerOne == 9);
{
c.clear();
c.println("Player One Won!");
}
if(playerOne == 4 && playerOne == 5 && playerOne == 6);
{
c.clear();
c.println("Player One Won!");
}
if(playerOne == 7 && playerOne == 8 && playerOne == 9);
{
c.clear();
c.println("Player One Won!");
}*/
}
}
Upvotes: 0
Views: 191
Reputation: 91
I believe that everytime its player one (or two) turn, you are overwriting the variable playerOne / playerTwo. Thats why no one ever wins.
To fix this you can make 2 arrays to store each players turn, and always append their turns. (google how to do that) also you would have to replace all these checks to check if the array includes the numbers. (also google)
if(playerOne == 3 && playerOne == 6 && playerOne == 9);
or a whole different,(probably easier), approach would be to have 9 variables for each field on the board all valued 0. then after each turn set the variable to 1 or 2 depending on who played and where they played. then after each turn perform a check to see if a row is all 1s, or all 2s
Upvotes: 2