c++写的井字棋(tic_tac_toe)

看见网上的好多算法 参差不齐,几百行的判断,懒得抄了  果断自己写一个

这个算法可优化的地方在于,稍微改一下就可以封装做成类,但是我不想封装了,有需要的自己封装

拿走的同学点个赞?有什么看法直接提出呗。

#include <iostream>
#include <time.h>
#include <random>
using namespace std;
void print(int s[3][3]);
bool check(int s[3][3]);
bool isEmpty(int s[3][3],int num);
bool isFull(int s[3][3]);
bool value(int s[3][3],int num,int flag);
int play(int s[3][3],int flag); //结束返回1  否则返回0
void MakeNull(int s[3][3]);
int main()
{
    int s[3][3] = {};
    int flag = 1;//选择先手
    print(s);
    while(flag)
    {
        MakeNull(s);
    cout<<"请选择先手 电脑先手-- 2  你先手-- 1  退出请输入:0\n";
    cin>>flag;
    while(flag)
     {
        if(!isFull(s))
        {
         if(flag==1)
            {
                if(!play(s,flag))
                    flag=2;
                else
                    break;
            }
            if(flag==2)
            {
                if(!play(s,flag))
                flag=1;
                else
                    break;
            }
        }
        else
            break;
    }

        cout<<"继续游戏请输入1,退出请输入0\n";
        cin>>flag;
    }
    if(flag==0)
    {
        cout<<"游戏结束!\n";
        exit(0);
    }
}

void print(int s[3][3])
{
        cout << "-------" << endl;
    for(int x = 0; x < 3; x++)
    {
        cout << "|";
        for(int y = 0; y < 3; y++)
        {
            if(s[x][y] == 0)
                cout << " ";
            else if(s[x][y] == 1)
                cout << "O";
            else if(s[x][y] == 2)
                cout << "X";
            cout << "|";
        }
        cout << endl << "-------" << endl;
    }
    cout << endl;
}
//检查是否结束
bool check(int s[3][3])
{
    //行列检查
    for(int x = 0;x<3;x++)
    {
        if(s[x][0]==s[x][1]&&s[x][1]==s[x][2]&&s[x][2]!=0)
            return true;
        if(s[0][x]==s[1][x]&&s[1][x]==s[2][x]&&s[2][x]!=0)
            return true;
    }
    //对角检查
    if(s[0][0]==s[1][1]&&s[1][1]==s[2][2]&&s[2][2]!=0)
        return true;
    if(s[0][2]==s[1][1]&&s[1][1]==s[2][0]&&s[2][0]!=0)
        return true;
    return false;
}
//检查是否满
bool isFull(int s[3][3])
{
    int full_flag = 1;
    for(int x=0;x<3;x++)
    {
        for(int y = 0;y<3;y++)
        {
            if(s[x][y]==0)
                full_flag=0;
        }
    }
    return full_flag;
}
//检查该处是否为空
bool isEmpty(int s[3][3],int num)
{
    if(num>9||num<1)
    {
        cout<<"输入错误!\n";
        return 0;
    }
    else
    {
    num=num-1;
    if(s[num/3][num%3]==0)
        return true;
    else
        return false;
    }
}
bool value(int s[3][3],int num,int flag) //flag用来判断是谁传进来的值
{
        num=num-1;
        s[num/3][num%3]=flag;

}

/*
*游戏运行
*传进判断参数、看谁先手进行一次判断其后进行循环即可
*/
int play(int s[3][3],int flag)//flag第一个传入的参数 判断是电脑先手还是人先手 人是1 电脑是2
{
  //不用判断是不是有效的flag 因为在case那里已经判断过了
  int location;
  srand(time(0));
    if(flag==1)
    {
        //执行一遍 然后跳到循环
       while(1){
        cout<<"请输入你的位置1-9:";
        cin>>location;
        if(isEmpty(s,location)&&!isFull(s))
        {
            value(s,location,flag);
            print(s);
            break;
        }
       }

    }
    else
    {
        //执行一遍 然后跳到循环
        if(!isFull(s))
        {
            while(1)
            {
            location = rand()%9+1;
            if(isEmpty(s,location))
               {
            value(s,location,flag);
            print(s);
            break;
                }
            }
        }

    }
    if(!isFull(s))
    {
        if(check(s))
        {
            if(flag==1)
            {
                cout<<"游戏结束!你赢了\n";
                return 1;
            }
            else
            {
                cout<<"游戏结束!你输了\n";
                return 1;
            }

        }
    }
    if(isFull(s))
    {
        cout<<"游戏结束,你们平局!\n";
        return 1;
    }
    return 0;
}
void MakeNull(int s[3][3])
{
    for(int i = 0;i<3;i++)
    {
        for(int j = 0 ;j<3 ;j++)
        {
            s[i][j]=0;
        }
    }
}

 

 

 

 

 


效果图:

 

可以不断玩下去,挺好玩的。

 

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的井字棋 Java 代码示例: ```java import java.util.Scanner; public class TicTacToe { private char[][] board; // 棋盘 private char currentPlayer; // 当前玩家 public TicTacToe() { board = new char[3][3]; currentPlayer = 'X'; initializeBoard(); } // 初始化棋盘 public void initializeBoard() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { board[i][j] = '-'; } } } // 打印棋盘 public void printBoard() { System.out.println("-------------"); for (int i = 0; i < 3; i++) { System.out.print("| "); for (int j = 0; j < 3; j++) { System.out.print(board[i][j] + " | "); } System.out.println(); System.out.println("-------------"); } } // 切换玩家 public void changePlayer() { currentPlayer = (currentPlayer == 'X') ? 'O' : 'X'; } // 检查是否已经有人胜利 public boolean checkForWin() { return (checkRowsForWin() || checkColumnsForWin() || checkDiagonalsForWin()); } // 检查行是否有胜利者 public boolean checkRowsForWin() { for (int i = 0; i < 3; i++) { if (checkRowCol(board[i][0], board[i][1], board[i][2])) { return true; } } return false; } // 检查列是否有胜利者 public boolean checkColumnsForWin() { for (int i = 0; i < 3; i++) { if (checkRowCol(board[0][i], board[1][i], board[2][i])) { return true; } } return false; } // 检查对角线是否有胜利者 public boolean checkDiagonalsForWin() { return (checkRowCol(board[0][0], board[1][1], board[2][2]) || checkRowCol(board[0][2], board[1][1], board[2][0])); } // 检查行或列是否有连续三个相同的字符 public boolean checkRowCol(char c1, char c2, char c3) { return ((c1 != '-') && (c1 == c2) && (c2 == c3)); } // 下棋 public void placeMark(int row, int col) { if ((row >= 0) && (row < 3) && (col >= 0) && (col < 3)) { if (board[row][col] == '-') { board[row][col] = currentPlayer; } } } // 运行游戏 public void play() { Scanner scanner = new Scanner(System.in); boolean gameOver = false; System.out.println("Let's play Tic Tac Toe!"); while (!gameOver) { System.out.println("Player " + currentPlayer + ", enter a row (0, 1, or 2): "); int row = scanner.nextInt(); System.out.println("Player " + currentPlayer + ", enter a column (0, 1, or 2): "); int col = scanner.nextInt(); placeMark(row, col); printBoard(); if (checkForWin()) { System.out.println("Congratulations! Player " + currentPlayer + " wins!"); gameOver = true; } else if (boardFull()) { System.out.println("Game over! It's a tie!"); gameOver = true; } else { changePlayer(); } } } // 检查棋盘是否已满 public boolean boardFull() { boolean full = true; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (board[i][j] == '-') { full = false; } } } return full; } public static void main(String[] args) { TicTacToe game = new TicTacToe(); game.play(); } } ``` 这个示例中,我们创建了一个 `TicTacToe` 类,表示一个井字棋游戏。在 `TicTacToe` 类中,我们定义了一些方法,如 `initializeBoard` 方法用于初始化棋盘,`printBoard` 方法用于打印棋盘,`changePlayer` 方法用于切换玩家,`checkForWin` 方法用于检查是否已经有人胜利等等。我们还在 `main` 方法中创建了一个 `TicTacToe` 对象并调用了 `play` 方法来运行游戏。 在游戏运行过程中,我们使用 `Scanner` 类来读取玩家的输入。玩家在每轮中输入一个行号和列号,程序会根据这个输入来下棋。程序会在每轮结束时检查是否有人胜利或棋盘已满,如果有人胜利或棋盘已满,游戏结束。如果没有人胜利或棋盘未满,程序会切换到下一个玩家并进行下一轮游戏。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值