三连棋小游戏

Augustus
• 阅读 159
#ifndef TicTacToe_H
#define TicTacToe_H
#include<array>
class TicTacToe
{
private:
    static std::array<std::array<int, 3>, 3>T;
    int num;
public:
    TicTacToe(int);
    static bool win();
    void show();
    bool play(int,int);
};
#endif
#include"head.h"
#include<iostream>
using std::array;
array<array<int, 3>, 3>TicTacToe::T{};

TicTacToe::TicTacToe(int n):num(n){}
bool TicTacToe::win()
{
    for (int i = 0; i < 3; i++)
    {
        if (T[i][1] == T[i][2] && T[i][0] == T[i][2] && T[i][1] != 0)
            return true;
    }
    for (int i = 0; i < 3; i++)
    {
        if (T[0][i] == T[1][i] && T[1][i] == T[2][i] && T[0][i] != 0)
            return true;
    }
    if(((T[0][0] == T[1][1] && T[1][1] == T[2][2])|| (T[2][0] == T[1][1] && T[1][1] == T[0][2]))&& T[1][1])
        return true;
    return false;
}
void TicTacToe::show()
{
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            std::cout << T[i][j] << " ";
        }
        std::cout << std::endl;
    }
}
bool TicTacToe::play(int a, int b)
{

    if ((a > 0 && a < 4 && b>0 && b < 4)&& T[a - 1][b - 1] == 0)
    {
        T[a - 1][b - 1] = this->num;
        show();
        return true;
    }
    else
    {
        std::cout << "Please enter a valid position!" << std::endl;
        return false;
    }

    if (win())
    {
        std::cout << "Player" << this->num << " win!" << std::endl;
    }
}
#include<iostream>
#include"head.h"
#include<array>
using std::cin;
using std::cout;
using std::endl;
int main()
{
    TicTacToe player1(1);
    TicTacToe player2(2);
    int turn;
    cout << "Please enter 1 or 2 to decide who to start." << endl;
    cin >> turn;
    if (turn != 1 && turn != 2)
    {
        cout << "You enter a wrong number!Enter again." << endl;
        cin >> turn;
    }
    int final = turn;
    while (TicTacToe::win() == false)
    {
        std::cout << "Please enter two integers between 1 and 3 to represent the position of tictactoe." << std::endl;
        int a, b;
        cin >> a >> b;
        if (turn % 2 == 1)
        {
            if (player1.play(a, b) == false)
                continue;
        }
        else if (turn % 2 == 0)
        {
            if (player2.play(a, b) == false)
                continue;
        }
        turn++;
        if ((turn - final == 9) && (TicTacToe::win() == false))
        {
            cout << "No one win." << endl;
            break;
        }
    }
}
点赞
收藏
评论区
推荐文章
MaxSky MaxSky
3年前
JavaScript 笛卡尔积算法,可用于商品 SKU 计算
代码示例:Demo演示:jsfunctioncalcDescartes(array){if(array.length<2)returnarray0||;return.reduce.call(array,function(col,set){varres;
Wesley13 Wesley13
2年前
FLV文件格式
1.        FLV文件对齐方式FLV文件以大端对齐方式存放多字节整型。如存放数字无符号16位的数字300(0x012C),那么在FLV文件中存放的顺序是:|0x01|0x2C|。如果是无符号32位数字300(0x0000012C),那么在FLV文件中的存放顺序是:|0x00|0x00|0x00|0x01|0x2C。2.  
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
PHP创建多级树型结构
<!lang:php<?php$areaarray(array('id'1,'pid'0,'name''中国'),array('id'5,'pid'0,'name''美国'),array('id'2,'pid'1,'name''吉林'),array('id'4,'pid'2,'n
Stella981 Stella981
2年前
Cocos2D
在Cocos2dx的开发中,如果你使用的xcode4.6,有时候希望使用C11标准库的东西,比如std::array,如果直接,include<array将会出现无法找到array的问题。这个时候我们就要对我们的xcode进行一下简单的配置。在BuildingSettings里面,首先点击All,然后在"
Stella981 Stella981
2年前
JVM 字节码指令表
字节码助记符指令含义0x00nop什么都不做0x01aconst\_null将null推送至栈顶0x02iconst\_m1将int型1推送至栈顶0x03iconst\_0将int型0推送至栈顶0x04iconst\_1将int型1推送至栈顶0x05ic
Wesley13 Wesley13
2年前
JS自定义结构体数组
废话不多说,直接上代码<script   vars\_keySearch{     key\_name:Array(),     key\_index:Array(),     key\_count:Array(),     key\_scount:Array()  };    for
Wesley13 Wesley13
2年前
PHP二维数据排序,二维数据模糊查询
一、因为项目中的一个报表需要合并三个表的数据,所以分表查询再合并数据,利用PHP数组函数进行排序,搜索。三表合并后的数组结构如下:Array(0Array(history_id12sla_group_
Stella981 Stella981
2年前
IE浏览器不兼容indexOf问题
if(!Array.prototype.indexOf){Array.prototype.indexOffunction(elt/,from/){varlenthis.length0;varfromNumber(arguments1
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Augustus
Augustus
Lv1
计算机专业学生
文章
3
粉丝
2
获赞
2