[英]How would one make it so that the program can tell you if player one or two have won the game?
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.这是学校的最后总结(是的,这是值得的最后一个项目,因为我的编码 class 去年因为 Covid 而被淘汰)并且我尽可能远离循环,我想知道如何让这个说玩家一和二有韩元。 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.顺便说一句,这是在控制台 HSA 中,所以我的能力有相当大的参数,而且我不擅长编码的事实也阻碍了我。 I more did this class for leisure because I enjoyed the little coding we got to do online last year.我做这个 class 更多是为了休闲,因为我喜欢我们去年在网上做的小编码。 We used Chromebook's to code and you can probably imagine how awful that was我们使用 Chromebook 进行编码,您可以想象那是多么糟糕
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!");
}*/
}
} }
I believe that everytime its player one (or two) turn, you are overwriting the variable playerOne / playerTwo.我相信每次它的玩家一(或二)回合,你都会覆盖变量 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.要解决此问题,您可以制作 2 个 arrays 来存储每个玩家的回合,并始终 append 存储他们的回合。 (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.或者完全不同(可能更容易),方法是为棋盘上的每个字段设置 9 个变量,所有变量的值都为 0。然后在每一轮之后,根据谁玩以及他们玩的位置将变量设置为 1 或 2。 then after each turn perform a check to see if a row is all 1s, or all 2s然后在每轮之后执行检查以查看一行是否全为 1 或全为 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.