求一个五子棋程序设计实验报告

2024-12-23 05:12:07
推荐回答(1个)
回答1:

我给你一个c++五子棋的程序mian.cpp#include
#include
#include
#include
#include
#include "pl.h"
using namespace std;pl game;
pl game2;void srandput(int &x,int &y) {
srand(time(NULL));
do {
x = rand() % 20;
y = rand() % 20;
} while (!game.isok(x,y));
}void scanfput(int &x,int &y) {
do {
scanf("%d %d",&x,&y);
} while (!game.isok(x,y));
}int main() {
int x,y;
char who = com;
x = y = -1;
do {
if (who == com) who = me;
else who = com;
if (who == me) {
game.getxy(x,y);
printf("Com put chess in (%d,%d)\n",x,y);
game.printmap();
if (game.isend(x,y,com)) {
who = com;
break;
} game.sendxy(x,y);
printf("I put chess in (%d,%d)\n",x,y);
game.printmap();
if (game.isend(x,y,me)) break;
//system("pause");
}
else {
//srandput(x,y);
scanfput(x,y);
//game2.getxy(x,y);
//game2.sendxy(x,y);
}
} while (true);
if (who == me) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),dwMe);
puts("You Win, Computer Lose.");
}
else {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),dwCom);
puts("Computer Win, You Lose.");
}
printf("Use %d Steps\n",game.getstep());
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),dwWhite);
} pi.hconst DWORD dwWhite = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN;
const DWORD dwMe = FOREGROUND_GREEN;
const DWORD dwCom = FOREGROUND_RED;
//■□○●
char rec1[] = {-95,-10,0};
char rec2[] = {-95,-11,0};
char cir1[] = {-95,-16,0};
char cir2[] = {-95,-15,0};
//////////////////////////////////////////////////////////////////////////
//class pl
//////////////////////////////////////////////////////////////////////////
const int NMAX = 20;
const int MAX = 1200;
const char blank = '0';
const char me = '1';
const char com = '2';class pl {
public:
void sendxy(int &,int &);
void getxy(int,int);
void initscore();
void refreshscore(char,int,int);
void calscore(char);
void solve(int &,int &);
void printmap();
bool isok(int,int);
bool isend(int,int,char);
int getstep();
pl();
private:
int usestep;
int wincount;
char chess[NMAX][NMAX];
int win[2][MAX];
//ps,pt -> me
//cs,ct -> com
int ps[NMAX][NMAX],
cs[NMAX][NMAX];
bool pt[NMAX][NMAX][MAX],
ct[NMAX][NMAX][MAX];
};pl::pl() {
memset(chess,blank,sizeof(chess));
memset(pt,false,sizeof(pt));
memset(ct,false,sizeof(ct));
memset(win,0,sizeof(win));
wincount = 0;
usestep = 0;
initscore();
}void pl::printmap() {
int i,j; for (i=0;i printf("\t\t\t%d\t",i);
for (j=0;j if (chess[i][j] == me) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),dwMe);
printf("%s",cir2);
}
else if (chess[i][j] == com) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),dwCom);
printf("%s",cir2);
}
else printf("%s",rec2);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),dwWhite);
}
putchar('\n');
}
printf("\n\t\t\t\t");
for (i=0;i putchar('\n');
}void pl::getxy(int x,int y) {
if (x==-1 && y==-1) return;
refreshscore(com,x,y);
}bool pl::isok(int x,int y) {
return (chess[x][y] == blank);
}bool pl::isend(int x,int y,char who) {
int i,j;
int l,r,u,d,c1,c2,c3,c4;
if (x==-1 && y==-1) return false;
l = r = u = d = 0;
c1 = c2 = c3 = c4 = 0;
for (i=1;i<5 && x+i < NMAX;i++) {
if (who == chess[x+i][y]) u ++;
else break;
}
for (i=1;i<5 && x-i >= 0;i++) {
if (who == chess[x-i][y]) d ++;
else break;
}
for (i=1;i<5 && y+i < NMAX;i++) {
if (who == chess[x][y+i]) r ++;
else break;
}
for (i=1;i<5 && y-i >= 0;i++) {
if (who == chess[x][y-i]) l ++;
else break;
}
for (i=1;i<5 && x+i < NMAX && y+i < NMAX;i++) {
if (who == chess[x+i][y+i]) c1 ++;
else break;
}
for (i=1;i<5 && x+i < NMAX && y-i >= 0;i++) {
if (who == chess[x+i][y-i]) c2 ++;
else break;
}
for (i=1;i<5 && x-i >= 0 && y-i >= 0;i++) {
if (who == chess[x-i][y-i]) c3 ++;
else break;
}
for (i=1;i<5 && x-i >= 0 && y+i < NMAX;i++) {
if (who == chess[x-i][y+i]) c4 ++;
else break;
}
return ( (u+1+d)>=5 || (l+1+r)>=5 || (c1+1+c3)>=5 || (c2+1+c4)>=5 );
}void pl::sendxy(int &x, int &y) {
solve(x,y);
usestep ++;
}//枚举赢的局面
void pl::initscore() {
int i,j,k;
//竖
for (i=0;i for (j=0;j for (k=0;k<5;k++) pt[j+k][i][wincount] = ct[j+k][i][wincount] = true;
wincount ++;
}
}
//横
for (i=0;i for (j=0;j for (k=0;k<5;k++) pt[i][j+k][wincount] = ct[i][j+k][wincount] = true;
wincount ++;
}
}
//斜1
for (i=0;i for (j=0;j for (k=0;k<5;k++) pt[j+k][i+k][wincount] = ct[j+k][i+k][wincount] = true;
wincount ++;
}
}
//斜2
for (i=0;i for (j=NMAX-1;j>=4;j--) {
for (k=0;k<5;k++) pt[j-k][i+k][wincount] = ct[j-k][i+k][wincount] = true;
wincount ++;
}
}
}//计算全局位置的分值
void pl::calscore(char who) {
bool (*table)[NMAX][MAX];
int (*score)[NMAX];
int iwho;
if (who == me) {
table = pt;
score = ps;
iwho = 1;
}
else {
table = ct;
score = cs;
iwho = 0;
}
srand(time(NULL));
int i,j,k;
for (i=0;i for (j=0;j score[i][j] = 0;
if (chess[i][j] == blank) {
for (k=0;k if (!table[i][j][k]) continue;
//分值的大小影响棋子位置的选择
srand(rand());
switch (win[iwho][k]) {
case 1:
score[i][j] += 1;
break;
case 2:
score[i][j] += 10;
break;
case 3:
score[i][j] += 100;
break;
case 4:
score[i][j] += 1000;
}
}
}
}
}
}void pl::solve(int &x, int &y) {
calscore(me);
calscore(com);
int i,j;
int me_score,com_score;
int me_x,me_y,com_x,com_y;
me_score = com_score = -1;
//选取我方或对方得分最大值
for (i=0;i for (j=0;j if (chess[i][j] == blank) {
if (me_score < ps[i][j]) {
me_score = ps[i][j];
me_x = i;
me_y = j;
}
if (com_score < cs[i][j]) {
com_score = cs[i][j];
com_x = i;
com_y = j;
}
}
}
}
if (me_score > com_score) {
x = me_x;
y = me_y;
}
else {
x = com_x;
y = com_y;
}
refreshscore(me,x,y);
}//更新分数
void pl::refreshscore(char who,int x,int y) {
bool (*table)[NMAX][MAX];
bool (*antitable)[NMAX][MAX];
int iwho;
if (who == me) {
table = pt;
antitable = ct;
iwho = 1;
}
else {
table = ct;
antitable = pt;
iwho = 0;
}
chess[x][y] = who;
for (int i=0;i //一方位置能胜利次数
if (table[x][y][i] && win[iwho][i] < INT_MAX) win[iwho][i] ++;
if (antitable[x][y][i]) {
//另一方无法放置
antitable[x][y][i] = false;
win[1-iwho][i] = INT_MAX;
}
}
}int pl::getstep() {
return usestep;
}