首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >一种简单的Tic-Tac-Toe游戏人工智能

一种简单的Tic-Tac-Toe游戏人工智能
EN

Code Review用户
提问于 2019-12-24 07:06:08
回答 1查看 325关注 0票数 1

这是一个AI类,它以董事会的数组为论据,在评估董事会之后发挥最好的一步,并获得一个胜率。我还没有使用任何java包来完成这个任务。我如何制作一棵遗传树并计算给定情况下的所有可能性。

代码语言:javascript
复制
public class AI
{
    int turn,ID;
    AI(int pID)
    {
        ID=pID;
        turn=0;
    }

    int getWinRate(int t[])
    { 
        int comb[][] = {{1,2,3},{4,5,6},{7,8,9},{1,4,7},{2,5,8},{3,6,9},{1,5,9},{3,5,7}} ;
        int AIwin,oppWin,empty,j,d;
        int WinRate = 0;
        for(int i=0;i<8;i++)  //Count AI win combinations
        {
            AIwin=oppWin=empty=0;
            for(j=0;j<3;j++)
            {
                d= comb[i][j]-1;
                 if(t[d]==2)
                 AIwin++;
                 if(t[d]==1)
                 oppWin++;
                 if(t[d]==0)
                 empty++;
            }
            //Changing win rate according to win conditions
            if(AIwin==3)        
            WinRate+=100;
            if(AIwin==2 && empty==1)
            WinRate+=3;
            if(oppWin==2 && empty==1)
            WinRate-=10;
        }
        return WinRate;
    } 
    void play(int t1[])
    {
        int t[]=t1.clone();
        int i,j,c=0;
        for(i=0;i<9;i++){
            if(t[i]==0)
            c++;
        }
        int[] moves=new int[c];
        c=0;
        for(i=0;i<9;i++)
        {
            if(t[i]==0)
           { moves[c++]=i;}
        }
        int rate,boardcopy[]=t.clone();boardcopy[moves[0]]=2;
        int bestMove=moves[0],maxRate=getWinRate(boardcopy);
        for(i=0;i<moves.length;i++)
        {
            boardcopy=t.clone();
            boardcopy[moves[i]]=2;
            rate=getWinRate(boardcopy);
            if(rate>maxRate)
            {
                maxRate=rate;
                bestMove=moves[i];
            }
        }
        System.out.println("The Mighty AI plays : "+(bestMove+1));
        turn++;
        t1[bestMove]=2;
    }
}
EN

回答 1

Code Review用户

发布于 2019-12-24 15:00:28

我不知道实际问题是什么,但我认为您希望有人以一般的方式检查您的代码。

您应该始终使用显式而不是隐式的作用域.

代码语言:javascript
复制
int getWinRate(int t[]){} //implicit scoping (not recommended)

public int getWinRate(int t[]){} //explicit scoping (recommended)

每行定义单变量、字段或方法,以获得更清晰的代码。

代码语言:javascript
复制
int AIwin,oppWin,empty,j,d; // not recommended

int oppWin; // recommended
int empty;
int j;
int d;

类名以大写字母和字段开头,方法变量名以小写字母开头。

代码语言:javascript
复制
int WinRate = 0; // not recommended

int winRate = 0; // recommended

for循环中定义迭代器,如果在计算中不使用数组索引,则可能应该使用foreach函数.

代码语言:javascript
复制
int i;

for(i=0;i<9;i++) // not recommended
{
    if(t[i]==0)
    { moves[c++]=i;}
} 

for(int i = 0; i < 9; i++) // recommended
{
    if(t[i] == 0)
    {moves[c++] = i;}
} 

for(int i : t) // recommended foreach function
{
    if(i == 0) // i - is not a iterator, but a array member
    {
         // Do some thing with i
    }
} 

总的来说,代码看起来非常复杂,因为它是一个简单的任务。希望这能帮助你提高自己的编码能力。

我建议您使用GitHub,如果您还没有使用它,并且比在市场页面上,您应该订阅CodeBeatCodacyBetterCodeHub自动代码评审应用程序。这是免费的公共存储库。这很有帮助。

票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/234576

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档