GUI井字游戏:Java的 [英] GUI Tic Tac Toe game : Java

查看:321
本文介绍了GUI井字游戏:Java的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我基本上都添加了一个面板中,通过对他们没有文字9个按键。当一个人被点击,它的文字更改为X或Ø根据玩家的把它。唯一的问题我已经是:

So I basically have 9 buttons added in a Panel with no text on them. Once one is clicked , it's text changes to X or O according to which players's turn it is. The only problem I have is:

  • 如果我玩游戏也运行得很好。

  • If I playing the game about 2 to 3 times it runs just fine .

在一些重放它开始呈现出不恰当的胜利消息。

After a few replays it starts showing improper win messages.

时有什么错我检查是否有人已经赢得了呀?下面是一个仅仅包含PVP模式的类。该注释掉的部分是获胜的条件。

Is there something wrong with the way I check if someone has won? Below is just the class containing the Pvp mode. The commented out part is the winning conditions.

推荐答案

0 1 2
3 4 5
6 7 8

0 1 2
3 4 5
6 7 8

您缺少1,4,7。因此,这将使得一些比赛出现双赢的错误。

You are missing 1,4,7. So this will make some games show up win errors.

我也建议像这样的检查:     INT I;

I also suggest a check like this: int i;

//0,1,2 - ,3,4,5 - 6,7,8
for(i =0; i<7; i+=3){
    if(grid[i]=='X' || grid[i]=='O' &&(grid[i] == grid[i+1] == grid[i+2])){
        break;
    }
 }

// 0,3,6 - 1,4,7 - 2,5,6
for(i =0; i<3; i++){
    if (grid[i]=='X' || grid[i]=='O' &&grid[i] == grid[i+3] == grid[i+6]){
    break;
    }
 // plus the two diagonal checks.
}


    int yes;
    yes = JOptionPane
        .showOptionDialog(
            null,
            "The game is over! " + grid[i] + " have won! Do you want to play again?",
                "Game over", JOptionPane.YES_NO_OPTION,
                 // etc..

这会为你节省至少200线code。

This will save you at least 200 lines of code.

我也想不明白自己在做什么用计数。

I also do not understand what you are doing with count.

这篇关于GUI井字游戏:Java的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆