简体   繁体   English

如何做到这一点,以便程序可以告诉您一号或二号玩家是否赢得了比赛?

[英]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.

相关问题 您将如何重写这3个函数,以便一个函数可以调用它? - How would you rewrite these 3 functions so that it can called by one function? 您如何判断一个div标签/元素是否在另一个标签/元素之上? - How can you tell if one div tag/element is over another? 如何制作两个 div,只显示一个,并且可以实时交换? - How can I make two divs, so that only one will be displayed, and they can be swapped in real time? 如何使一个组件具有全屏背景图像? - How would I make one component have a fullscreen background image? 如何切换播放器变量以在播放器一和播放器二之间切换? - How can I toggle my player variable to switch between player one and player two? 编程由一个人控制的棋盘游戏的最佳方法是什么? - What would be the best way to program a board game controlled by one person? AngularJS如何从一个网站运行两个控制器,并更改其中一个 - AngularJS How would one run two controllers from one website, and have one of them change 你怎么能告诉 reactJs 每个复选框都是唯一的,所以当我更改一个框的 checked 属性时,它不会勾选所有其他框? - How can you tell reactJs that each checkbox is unique so when I change the checked attribute for one box it doesn't tick all the other boxes? XO 游戏(我如何证明玩家一(p1)或玩家二(p2)在达到 (v​​ar winBoxes) 中的条件之一时获胜 - X O Game(how i prove that player one(p1) or player two(p2) win if he achive the one of the conditions in (var winBoxes) 可以将两个bookmarklet和一个Greasemonkey脚本合并为一个bookmarklet吗? 如果是这样,怎么样? - Can two bookmarklets and a Greasemonkey script be combined into one bookmarklet? If so, how?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM