井字棋游戏

#include "Board.h"

Board::Board(){
    size = 10;
    s = new char[9];
    set();                           //对于棋盘的初始化
}

char Board::getS(int i){
 return s[i];
}

int Board::getSize(){
 return size;
}
 
Board::Board(Board &c){
    size = c.size;
    s = new char[c.size];
    set();                          //对于棋盘的初始化
}
 
void Board::set(){                   //对于棋盘的初始化 ,将每个位置均编号,对于未落子的位置记为数字;
 for(int i=0;i<size;i++){
  s[i]= i +‘0‘;
 }
 count = 0;
}

int Board::show(int n){  
     if(n==0){
      cout<<"使用的是下面的这一个棋盘,相应数字对应于相应位置, 输入相应数字即可在相应位置落子"<<endl;
     }
     char w;                          //把数字转化为字符,对于棋局布置规避了数字表示,而采用
  w = n + ‘0‘;                     //数值得字符形式,避免数字使用时的不可知错误
  if(n>9||n<0){                    //在屏幕上九个位置对应于1-9;这里排除不合法输入
   cout<<" 这个输入不合规则,请重新选定落子位置!"<<endl;
   return -1;                    //位置非法,返回-1;对应输入重新输,传回twoplayer函数
  }
     if(s[n]==w||s[n]==‘0‘){
      if(count%2==0)               //应用奇数偶数之间交替实现落子交替
       s[n]=‘x‘;
         else
          s[n]=‘o‘;
    count++;                     //记录输入次数并记录交替
  
    cout<<endl;                  //显示格子并显示输出,显示棋局过程
       cout<<"                        "<<s[7]<<" || "<<s[8]<<" || "<<s[9]<<"  "<<endl; 
          cout<<"                     ****************"<<endl; 
       cout<<"                        "<<s[4]<<" || "<<s[5]<<" || "<<s[6]<<"  "<<endl;
          cout<<"                     ****************"<<endl; 
       cout<<"                        "<<s[1]<<" || "<<s[2]<<" || "<<s[3]<<"  "<<endl;
          cout<<endl<<endl; 
      }
      else {
       // cout<<"这个位置已经被占据了,请重新落子!!"<<endl;
        return -1;                     //位置被占据,不合法,返回-1,要求重新输入
      }
  
}
 
int  Board::reset(){                    //用于未分胜负或者一局完成时继续比赛的重置
    cout<<"  你想再来一局吗?"<<endl;
    int replay;
    cout<<"是(请输入 1 ) , 否 ( 请输入 0 )"<<endl;
    cin>>replay;                        //对所以数据恢复初始状态
    while(replay!=0&&replay!=1){
       cout<<"选择输入不合理,确定是否再来一局,输入1(是)  0(否)";
    cin>>replay;
    }
       return replay;                      //返回数值,确定需要重新开始
}

int  Board::viewO(){
 
  if(s[1]==‘o‘&&s[2]==‘o‘&&s[3]!=‘x‘) return 3;
  else if(s[1]==‘o‘&&s[3]==‘o‘&&s[2]!=‘x‘) return 2;
  else if(s[3]==‘o‘&&s[2]==‘o‘&&s[1]!=‘x‘) return 1;
  else if(s[3]==‘o‘&&s[5]==‘o‘&&s[7]!=‘x‘) return 7;
  else if(s[7]==‘o‘&&s[5]==‘o‘&&s[3]!=‘x‘) return 3;
  else if(s[3]==‘o‘&&s[7]==‘o‘&&s[5]!=‘x‘) return 5;

else if(s[4]==‘o‘&&s[5]==‘o‘&&s[6]!=‘x‘) return 6;
  else if(s[4]==‘o‘&&s[6]==‘o‘&&s[5]!=‘x‘) return 5;
  else if(s[5]==‘o‘&&s[6]==‘o‘&&s[4]!=‘x‘) return 4;
     else if(s[7]==‘o‘&&s[8]==‘o‘&&s[9]!=‘x‘) return 9;
  else if(s[7]==‘o‘&&s[9]==‘o‘&&s[8]!=‘x‘) return 8;
  else if(s[8]==‘o‘&&s[9]==‘o‘&&s[7]!=‘x‘) return 7;

else if(s[1]==‘o‘&&s[4]==‘o‘&&s[7]!=‘x‘) return 7;
  else if(s[1]==‘o‘&&s[7]==‘o‘&&s[4]!=‘x‘) return 4;
  else if(s[7]==‘o‘&&s[4]==‘o‘&&s[1]!=‘x‘) return 1;
  else if(s[2]==‘o‘&&s[5]==‘o‘&&s[8]!=‘x‘) return 8;
  else if(s[8]==‘o‘&&s[2]==‘o‘&&s[5]!=‘x‘) return 5;
  else if(s[5]==‘o‘&&s[8]==‘o‘&&s[2]!=‘x‘) return 2;

else if(s[3]==‘o‘&&s[6]==‘o‘&&s[9]!=‘x‘) return 9;
  else if(s[6]==‘o‘&&s[9]==‘o‘&&s[3]!=‘x‘) return 3;
  else if(s[3]==‘o‘&&s[9]==‘o‘&&s[6]!=‘x‘) return 6;
  else if(s[1]==‘o‘&&s[5]==‘o‘&&s[9]!=‘x‘) return 9;
  else if(s[1]==‘o‘&&s[9]==‘o‘&&s[5]!=‘x‘) return 5;
  else if(s[9]==‘o‘&&s[5]==‘o‘&&s[1]!=‘x‘) return 1;
 
  else return 100; 
}

int Board::viewX(){
 
  if(s[1]==‘x‘&&s[5]==‘x‘&&s[9]!=‘o‘) return 9;
  else if(s[1]==‘x‘&&s[9]==‘x‘&&s[5]!=‘o‘) return 5;
  else if(s[9]==‘x‘&&s[5]==‘x‘&&s[1]!=‘o‘) return 1;
  else if(s[3]==‘x‘&&s[6]==‘x‘&&s[9]!=‘o‘) return 9;
  else if(s[3]==‘x‘&&s[9]==‘x‘&&s[6]!=‘o‘) return 6;
  else if(s[6]==‘x‘&&s[9]==‘x‘&&s[3]!=‘o‘) return 3;

else if(s[3]==‘x‘&&s[5]==‘x‘&&s[7]!=‘o‘) return 7;
  else if(s[7]==‘x‘&&s[5]==‘x‘&&s[3]!=‘o‘) return 3;
  else if(s[3]==‘x‘&&s[7]==‘x‘&&s[5]!=‘o‘) return 5;
     else if(s[1]==‘x‘&&s[2]==‘x‘&&s[3]!=‘o‘) return 3;
  else if(s[1]==‘x‘&&s[3]==‘x‘&&s[2]!=‘o‘) return 2;
  else if(s[3]==‘x‘&&s[2]==‘x‘&&s[1]!=‘o‘) return 1;
   
     else if(s[4]==‘x‘&&s[5]==‘x‘&&s[6]!=‘o‘) return 6;
  else if(s[4]==‘x‘&&s[6]==‘x‘&&s[5]!=‘o‘) return 5;
  else if(s[5]==‘x‘&&s[6]==‘x‘&&s[4]!=‘o‘) return 4;
  else if(s[7]==‘x‘&&s[8]==‘x‘&&s[9]!=‘o‘) return 9;
  else if(s[7]==‘x‘&&s[9]==‘x‘&&s[8]!=‘o‘) return 8;
  else if(s[8]==‘x‘&&s[9]==‘x‘&&s[7]!=‘o‘) return 7;
 
  else if(s[2]==‘x‘&&s[5]==‘x‘&&s[8]!=‘o‘) return 8;
  else if(s[8]==‘x‘&&s[2]==‘x‘&&s[5]!=‘o‘) return 5;
  else if(s[5]==‘x‘&&s[8]==‘x‘&&s[2]!=‘o‘) return 2;
  else if(s[1]==‘x‘&&s[4]==‘x‘&&s[7]!=‘o‘) return 7;
  else if(s[1]==‘x‘&&s[7]==‘x‘&&s[4]!=‘o‘) return 4;
  else if(s[4]==‘x‘&&s[7]==‘x‘&&s[1]!=‘o‘) return 1;
  else return 100;
   
}

#include<iostream>
#include<string>
 
using namespace std;

#ifndef BOARD
#define BOARD

class Board{
 private:
  char *s;
  int size;
  int count; 
    public:
     char getS(int i);
     int getSize();
        Board();
  Board(Board&); 
        int show(int n);
     void set();
     int reset();
     int viewO();
      int viewX();    
};

#endif

#include<iostream>
#include "GamePlay.h"

GamePlay::GamePlay(){
    pvc = new  Play_vs_Computer;
    b = new Play_vs_Play;
    modal();
};
GamePlay::~GamePlay(){
 delete pvc;
 delete b;
}

void GamePlay::modal(){
 int n;

cout<<"                 ****欢迎参加本款五子棋的使用****"<<endl;
 cout<<"****以下是棋盘的位置分布,每个数字对应于一个位置,输入相应数字即可在相应位置落子****"<<endl;
    cout<<endl;                  //显示格子并显示输出,显示棋局过程
       cout<<"                        "<<7<<" || "<<8<<" || "<<9<<"  "<<endl; 
          cout<<"                     ****************"<<endl; 
       cout<<"                        "<<4<<" || "<<5<<" || "<<6<<"  "<<endl;
          cout<<"                     ****************"<<endl; 
       cout<<"                        "<<1<<" || "<<2<<" || "<<3<<"  "<<endl;
          cout<<endl<<endl; 
 cout<<"             ----使用‘O’或 ‘X’连成一线即为赢家----"<<endl;
   
   
 cout<<"人 人对战请输入--- 1 :"<<endl;
 cout<<"人 机对战请输入--- 2 :"<<endl;
 cin>>n;
 if(n!=1&&n!=2){
  int t;
  cout<<"选择不合理,请再输入一遍: ";
  cin>>t;
  n=t;
 }
 if(n==1){
  b->Play();
 }
 if(n==2){
  pvc->Play();
 }
 
 
}

#include<iostream>
#include<string>
#include "Judge.h"
#include "Board.h"
#include "Play_vs_Play.h"
#include "Play_vs_Computer.h"

using namespace std;

#ifndef GAMEPLAY
#define GAMEPLAY

class GamePlay{
 private:
  Play_vs_Play *b;
  Play_vs_Computer *pvc;
 public:
  GamePlay();
  ~GamePlay();
  void modal();
};

#endif

#include "Judge.h"

Judge::Judge(){
 score = 0;
}

Judge::Judge(Judge &a){
 score = a.score;
}

int Judge::getScore(){
 return score;
}

void Judge::addScore(){
 score += 2;
}

int Judge::judge(Board &board){                                
 
 if(board.getS(1)==‘o‘&&board.getS(2)==‘o‘&&board.getS(3)==‘o‘){
     addScore();   return -1;                
    }
    if(board.getS(4)==‘o‘&&board.getS(5)==‘o‘&&board.getS(6)==‘o‘){
     addScore();   return -1;                      
    }
   
    if(board.getS(7)==‘o‘&&board.getS(8)==‘o‘&&board.getS(9)==‘o‘){
     addScore();   return -1;                      
    }
   
 if(board.getS(1)==‘o‘&&board.getS(5)==‘o‘&&board.getS(9)==‘o‘){
     addScore();   return -1;                      
    }
   
    if(board.getS(2)==‘o‘&&board.getS(5)==‘o‘&&board.getS(8)==‘o‘){
     addScore();   return -1;                      
    }
   
    if(board.getS(3)==‘o‘&&board.getS(5)==‘o‘&&board.getS(7)==‘o‘){
     addScore();   return -1;                      
    }

if(board.getS(1)==‘o‘&&board.getS(4)==‘o‘&&board.getS(7)==‘o‘){
     addScore();   return -1;                      
    }
   
    if(board.getS(2)==‘o‘&&board.getS(5)==‘o‘&&board.getS(8)==‘o‘){
     addScore();   return -1;                      
    }
   
    if(board.getS(3)==‘o‘&&board.getS(6)==‘o‘&&board.getS(9)==‘o‘){
     addScore();   return -1;                      
    }
   
   
   
    if(board.getS(1)==‘x‘&&board.getS(2)==‘x‘&&board.getS(3)==‘x‘){
     addScore();   return -2;                      
    }
    if(board.getS(4)==‘x‘&&board.getS(5)==‘x‘&&board.getS(6)==‘x‘){
     addScore();   return -2;                      
    }
   
    if(board.getS(7)==‘x‘&&board.getS(8)==‘x‘&&board.getS(9)==‘x‘){
     addScore();   return -2;                      
    }
   
 if(board.getS(1)==‘x‘&&board.getS(5)==‘x‘&&board.getS(9)==‘x‘){
     addScore();   return -2;                      
    }
   
    if(board.getS(2)==‘x‘&&board.getS(5)==‘x‘&&board.getS(8)==‘x‘){
     addScore();   return -2;                      
    }
   
    if(board.getS(3)==‘x‘&&board.getS(5)==‘x‘&&board.getS(7)==‘x‘){
     addScore();   return -2;                      
    }

if(board.getS(1)==‘x‘&&board.getS(4)==‘x‘&&board.getS(7)==‘x‘){
     addScore();   return -2;                      
    }
   
    if(board.getS(2)==‘x‘&&board.getS(5)==‘x‘&&board.getS(8)==‘x‘){
     addScore();   return -2;                      
    }
   
    if(board.getS(3)==‘x‘&&board.getS(6)==‘x‘&&board.getS(9)==‘x‘){
     addScore();   return -2;                      
    }
}

void Judge::setScore(int n){
 score = score + n;
}

#include<iostream>
#include "Board.h"

using namespace std;

#ifndef JUDGE
#define JUDGE

class Judge{
 private:
  int score;
 public:
  Judge();
  Judge(Judge&);
  void addScore();
  void setScore(int n);
  int getScore();
  int judge(Board &board);
};

#endif

#include<iostream>
#include<string>
#include <cstdlib>
#include<ctime>
#include "Play_vs_Computer.h"
#include "Board.h"

Play_vs_Computer::Play_vs_Computer(){
    name = "Unitil" ;
 Computername = "Computer_player";
 judger = new Judge;
 board = new Board; 
}

void Play_vs_Computer::setname(string a){
 name = a;
}

string Play_vs_Computer::getName(){
 return name;
}

void Play_vs_Computer::BuildPlayer(){
 string s1,s2;
    cout<<"请输入player号的名字:  ";
 cin>>s1;
    setname(s1);
}

void Play_vs_Computer::Play(){
 srand(time(0));
 BuildPlayer();
    int flag = 1;
 while(flag==1){
    board->set();
    board->show(0);
   
    cout<<"请玩家确认是否先手: 是(输入1) ————否(输入 0)"<<endl;
    int first;
    cin>>first;
    if(first!=1&&first!=0){
      cout<<"选择不合理,请重新输入: ";
      cin>>first;
    }
    if(first==1){
      cout<<"现在是["<<name<<"]先手: 使用的是( O )"<<endl;
    }
    if(first==0){
      cout<<"现在是["<<Computername<<"]先手:使用的是( O )"<<endl;
    }
       if(first==1){
              
        for(int ii=0;ii<9;ii++){         //九次落子交替
         int ts;
         if(ii%2==0){
             cout<<"请["<<name<<"]落子: ";
             cin>>ts;
             while(board->show(ts)==-1){         //判断落子位置是否被占据,占据则重输
                 cout<<"这个位置已经被占据了!!"<<endl;
                 int t;
                 cout<<"请["<<getName()<<"]重新落子: ";
                 cin>>t;
                 ts = t;
               }
        }
        else {
             cout<<"[Computer_player]落子: ";
             if(ii==1&&ts==5){
                  int b;
              b=rand()%9+1;
                          while(b%2!=1||b==5){
                            b=rand()%9+1;
                          }
                          board->show(b);
                }
             if(ii==1&&(ts==1||ts==3||ts==9||ts==7)) board->show(5);
             if(ii==1&&(ts==2||ts==4||ts==6||ts==8)) board->show(5);
                   if(ii>=3&&ii%2==1){
                       if(board->viewX()!=100){
                           ts = board->viewX();
                           board -> show(ts);
                       }
                       else if(board->viewO()!=100){
                           ts = board->viewO();
                           board -> show(ts);
                       }
                       else {
                int b;                            
                b=rand()%9+1;
                         while(board->show(b)==-1){
                         b=rand()%9+1;
                         }
                         cout<<b;
                       }
                   }
        }     
        if(judger->judge(*board)==-1||judger->judge(*board)==-2){
        
            if(judger->judge(*board)==-1) {
              cout<<name<<"胜利!!"<<endl;
            }
                  if(judger->judge(*board)==-2) {
                   cout<<Computername<<"胜利!!"<<endl;
                  } 
            flag = board->reset();
           break;
        }
    
        if(ii==8){
                cout<<"两位都非常厉害,然而并没有什么用,未分伯仲"<<endl;
                board->set();
                flag = board->reset();
              }
          
           }
         
       }
      
      
       if(first==0){
              
        for(int ii=0;ii<9;ii++){         //九次落子交替
         int ts;
         if(ii%2!=0){
             cout<<"请["<<name<<"]落子: ";
             cin>>ts;
             while(board->show(ts)==-1){         //判断落子位置是否被占据,占据则重输
                 cout<<"这个位置已经被占据了!!"<<endl;
                 int t;
                 cout<<"请["<<getName()<<"]重新落子: ";
                 cin>>t;
                 ts = t;
               }
        }
        else {
             cout<<"[Computer_player]落子: ";
       board->show(5);
       if(ii==2&&(ts==1||ts==3||ts==7||ts==9)){
          board->show(2);
       }
       if(ii==2&&(ts==2||ts==4)){
          board->show(9);
       }
                if(ii==2&&(ts==6||ts==8)){
          board->show(1);
       }
                   if(ii>2&&ii%2==0){
                       if(board->viewO()!=100){
                           ts = board->viewO();
                           board -> show(ts);
                       }
                       if(board->viewX()!=100){
                           ts = board->viewX();
                           board -> show(ts);
                       }
                   else {
                int b;                            
                b=rand()%9+1;
                         while(board->show(b)==-1){
                         b=rand()%9+1;
                         }
                         cout<<b;
                       }
                   }
        }     
        if(judger->judge(*board)==-1||judger->judge(*board)==-2){
        
            if(judger->judge(*board)==-2) {
              cout<<name<<"胜利!!"<<endl;
            } 
                  if(judger->judge(*board)==-1){
                   cout<<Computername<<"胜利!!"<<endl;
                  }
            flag = board->reset();
           break;
        }
    
        if(ii==8){
                cout<<"两位都非常厉害,然而并没有什么用,未分伯仲"<<endl;
                board->set();
                flag = board->reset();
              }
          
           }
         
       }
      
      
 }
 
 cout<<"Game Over!!"<<endl;
}

#include<iostream>
#include<string>
#include "Judge.h"
#include "Board.h"
#include "Play_vs_Play.h"

using namespace std;

#ifndef PLAY_VS_COMPUTER
#define PLAY_VS_COMPUTER

class Play_vs_Computer : public Play_vs_Play {
 private:
  string name;
  string Computername;
  Board *board;
  Judge *judger;
 public:
  Play_vs_Computer();
  void Play();
  void BuildPlayer();
  void setname(string a);
  string getName();
};

#endif

#include<iostream>
#include<string>
#include "Play_vs_Play.h"
#include "Board.h"

Play_vs_Play::Play_vs_Play(){
 name1 = "Untitle";
 name2 = "Untitle";
 judger = new Judge;
 board = new Board; 
}

void Play_vs_Play::setname1(string a){
 name1 = a;
}

void Play_vs_Play::setname2(string a){
 name2 = a;
}

void Play_vs_Play::BuildPlayer(){
 string s1,s2;
    cout<<"请输入player1号的名字:  ";
 cin>>s1;
 cout<<"请输入player2号的名字:  ";
 cin>>s2;
    setname1(s1);
    setname2(s2);
}

void Play_vs_Play::Play(){
 BuildPlayer();
    int flag = 1;
 while(flag==1){
    board->set();
    board->show(0);
   
    cout<<"请第一位玩家确认是否先手: 是(输入1) ————否(输入 0)"<<endl;
    int first;
    cin>>first;
    if(first!=1&&first!=0){
      cout<<"选择不合理,请重新输入: ";
      cin>>first;
    }
    if(first==1){
      cout<<"现在是["<<name1<<"]先手: 使用的是( O )"<<endl;
    }
    if(first==0){
      cout<<"现在是["<<name2<<"]先手:使用的是( X )"<<endl;
    }
     
    for(int ii=0;ii<9;ii++){         //九次落子交替
     int ts;
           if(first == 1 ){
              if(ii%2==0){
          cout<<"请["<<name1<<"]落子:";
        }
        else cout<<"请["<<name2<<"]落子:";
           }
     if(first == 0){
              if(ii%2!=0){
          cout<<"请["<<name1<<"]落子:";
        }
        else cout<<"请["<<name2<<"]落子:";
           }

cin>>ts;
     while(board->show(ts)==-1){         //判断落子位置是否被占据,占据则重输
        cout<<"这个位置已经被占据了,请重新落子!!"<<endl;
        int t;
      if(ii%2==0){
          cout<<"请["<<name1<<"重新落子:";
     }
     else cout<<"请["<<name2<<"]重新落子:";
        cin>>t;
        ts = t;
        }
       
     if(judger->judge(*board)==-1||judger->judge(*board)==-2){
        
         if(judger->judge(*board)==-1){
            cout<<name1<<"胜利!!"<<endl;
         }
        
         if(judger->judge(*board)==-2){
            cout<<name2<<"胜利!!"<<endl;
         }
      
        flag = board->reset();
       break;
     }
    
     if(ii==8){
              cout<<"两位都非常厉害,然而并没有什么用,未分伯仲"<<endl;
              board->set();
              flag = board->reset();
           }
          
       }
         
 }
 cout<<"Game Over!!"<<endl;
}

#include<iostream>
#include<string>
#include "Judge.h"
#include "Board.h"

using namespace std;

#ifndef Play_vs_play
#define Play_vs_play

class Play_vs_Play{
 private:
  string name1;
  string name2;
  Board *board;
  Judge *judger;
 public:
  Play_vs_Play();
  void Play();
  void BuildPlayer();
  void setname1(string a);
  void setname2(string a);
};

#endif

#include <iostream>
#include "Board.h"
#include "Play_vs_Play.h"
#include "Judge.h"
#include "GamePlay.h"
using namespace std;
int main(){
      GamePlay a;
   return 0;
}

时间: 2024-10-21 06:20:10

井字棋游戏的相关文章

人工智能博弈树算法做的井字棋游戏

不会输,超碉!井字棋这个游戏真是太无聊啦! 算法大概就是,有一个给状况进行估价的函数,深搜每种状况,假设每个人都按对自己最有利的方式走(假设玩家也是不傻),最后让电脑走出最有利的一步. 代码: 1 //#pragma comment(linker, "/STACK:102400000,102400000") 2 #include<cstdio> 3 #include<cmath> 4 #include<iostream> 5 #include<

C++井字棋游戏,DOS界面版

据说有一个能保证不败的算法.明天看看先再写个PVC版的. 正题.今天无聊写了个井字棋游戏,顺便逐渐让自己习惯良好的代码风格,放上来给新手学习学习. jzq2.cpp /* N字棋游戏PVP版,DOS版 本棋盘可扩充,仅仅需调整检測条件就可以,其它接口不需改变. 非人机对战型.PVP类型; @author:天下无双 @date:2014-5-25 @version:1.0 */ #include <iostream> #include <string> #define INVALID

井字棋游戏升级版 - TopTicTacToe项目 简介

一.游戏简介 井字棋是一款世界闻名的游戏,不用我说,你一定知道它的游戏规则. 这款游戏简单易学,玩起来很有意思,不过已经证明出这款游戏如果两个玩家都足够聪明的话, 是很容易无法分出胜负的,即我们得到的结果是平局. 我们的项目,就是井字棋游戏的升级版!游戏有九个小棋盘,每个棋盘构成了整体布局的一部分,要想获得游戏的胜利,你要把握整个局势才行! 二.亮点 创新 传统的井字棋只有九个格,玩法简单,但是变化也相当有限.初玩者很容易被这新颖的游戏吸引住,但是玩了一段时间后,很容易摸出规律,很轻松达到不败的

[CareerCup] 17.2 Tic Tac Toe 井字棋游戏

17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏,有下面几点需要考虑: 1. 判断是否能赢hasWon函数是调用一次还是多次,如果是多次,我们可能为了优化而需要加入一些预处理. 2. 井字棋游戏通常是3x3的大小,我们是否想要实现NxN的大小? 3. 我们需要在代码紧凑,执行速度和代码清晰之间做出选择. #include <iostream> #

[LeetCode] Design Tic-Tac-Toe 设计井字棋游戏

Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume the following rules: A move is guaranteed to be valid and is placed on an empty block.Once a winning condition is reached, no more moves is allowed.A player

[Swift]LeetCode348. 设计井字棋游戏 $ Design Tic-Tac-Toe

Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume the following rules: A move is guaranteed to be valid and is placed on an empty block.Once a winning condition is reached, no more moves is allowed.A player

Pascal小游戏 井字棋

一个很经典的井字棋游戏 Pascal源码Chaobs奉上 注意:1.有的FP版本不支持汉语,将会出现乱码.2.别想赢电脑了,平手不错了. 井字过三关: program TicTacToe; uses crt; var a:Array [1..3] of Array [1..3] of char; b:Array [1..3] of Array [1..3] of integer; i,n,g,e,p:integer; t:text; c:char; o:integer; r:integer; s

井字棋的最优策略竟是先占角!

http://www.guokr.com/article/4754/ 井字棋可能是最简单的棋类游戏了,它简单到了成年人之间玩几乎总是平局的地步.因此,这个游戏貌似最多只能哄哄小孩子.不过,对井字棋游戏中所有可能的情况进行一番细致的分析,你会发现一个你或许不会料到的惊人结论——先手的最优策略不是稳坐正中央,而是先占一个角! 几年前,果壳网小编曾经自己动手写过一个和人下井字棋的电脑程序,运行之后却发现电脑先走时总爱把第一步棋下在角上:检查程序代码许久后才意识到,电脑程序可能并没有问题.人们往往有一个

LeetCode 5275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game

地址 https://www.acwing.com/solution/LeetCode/content/6670/ 题目描述A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: 玩家轮流将棋子放在空方格 (” “) 上.第一个玩家 A 总是用 “X” 作为棋子,而第二个玩家 B 总是用 “O” 作为棋子.“X” 和 “O” 只能放在空方格中,而不能放在已经被占用的方格上.只要有 3 个相同的(非空)棋子排成一条直线(行.列.对角线)时,游戏结束.如果所有方块都放满棋子(不为