#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;
}