c五子棋課程設計報告
Ⅰ 求一個五子棋程序設計實驗報告
我給你一個c++五子棋的程序mian.cpp#include <cstdio>
#include <string>
#include <cstdlib>
#include <ctime>
#include <Windows.h>
#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<NMAX;i++) {
printf("\t\t\t%d\t",i);
for (j=0;j<NMAX;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<NMAX;i++) printf("%2d",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<NMAX;i++) {
for (j=0;j<NMAX-4;j++) {
for (k=0;k<5;k++) pt[j+k][i][wincount] = ct[j+k][i][wincount] = true;
wincount ++;
}
}
//橫
for (i=0;i<NMAX;i++) {
for (j=0;j<NMAX-4;j++) {
for (k=0;k<5;k++) pt[i][j+k][wincount] = ct[i][j+k][wincount] = true;
wincount ++;
}
}
//斜1
for (i=0;i<NMAX-4;i++) {
for (j=0;j<NMAX-4;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<NMAX-4;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<NMAX;i++) {
for (j=0;j<NMAX;j++) {
score[i][j] = 0;
if (chess[i][j] == blank) {
for (k=0;k<wincount;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<NMAX;i++) {
for (j=0;j<NMAX;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<wincount;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;
}
Ⅱ 如何寫出用c語言製作五子棋的報告
你去淘寶上看看有沒有,把"游戲設計」和「五子棋」作為關鍵字,上次我們老師讓作貪吃蛇我就在上面買到了,祝你好運。
Ⅲ 求助:C語言五子棋課程設計報告~~
到csdn找找看,應該有的,對於學計算機的很好的網站
Ⅳ 五子棋的課程設計思路
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>using namespace std;int ChessData[15][15] ={0};
int GuangbiaoData[2]={8,8};
int ChessStepData[255][2]={0};
void gotoxy(int x, int y) //gotoxy在TC中是在system.h庫文件里的一個函數,但是在VC中沒有這個函數,所以在網上找到了這個函數以實現同樣的功能。
//只有這一個函數是網上找的,別的全部我自己寫的。
{
COORD c;
c.X=x-1;
c.Y=y-1;
SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c);
}void GotoChess(int x,int y)
{
x=3*x-2;y=2*y-1;
gotoxy(x,y);
}void Move(int MoveData) //輸入參數為用戶輸入的方向(1表示上,2表示下,3表示左,4表示右)
{
switch (MoveData)
{
case 1:GuangbiaoData[1]-=1;break;
case 2:GuangbiaoData[1]+=1;break;
case 3:GuangbiaoData[0]-=1;break;
case 4:GuangbiaoData[0]+=1;break;
default:cout<<"Move函數出錯"<<endl;
}
GotoChess(GuangbiaoData[0],GuangbiaoData[1]);
}int Get(int *data) //該函數的功能是用戶的按鍵,並轉化為01234567(0表示輸入錯誤,1表示上,2表示下,3表示左,4表示右,5表示落子,6表示悔棋,7表示退出。)
{ //並返回輸入的用戶號碼(共同鍵返回3),錯誤則返回0
int temp;temp=getch();
if (temp==224)
{
temp=getch();
switch (temp)
{
case 72:*data=1;break;
case 80:*data=2;break;
case 75:*data=3;break;
case 77:*data=4;break;
default:printf("Get函數出錯");
}
return 2;
}
else
{
switch (temp)
{
case 'w':
case 'W':*data=1;return 1;break;
case 's':
case 'S':*data=2;return 1;break;
case 'a':
case 'A':*data=3;return 1;break;
case 'd':
case 'D':*data=4;return 1;break;
case 13 :*data=5;return 2;break;
case 32 :*data=5;return 1;break;
case 8 :*data=6;break;
case 27 :*data=7;break;
default:*data=0 ;return 0;break;
}
return 3;
}
}
void MoveToEnd()
{
gotoxy(1,30);
}
int LogicBeOut(int a,int b)
{
if (a==-1||a==15||b==-1||b==15) return 1;
else return 0;
}
int win(int v)
{
int i=1,j=1,a=0,b=0;
while (ChessData[a=GuangbiaoData[0]-i-1][b=GuangbiaoData[1]-i-1]==v*2&&!LogicBeOut(a,b)) i++;
while (ChessData[a=GuangbiaoData[0]+j-1][b=GuangbiaoData[1]+j-1]==v*2&&!LogicBeOut(a,b)) j++;
if (i+j-1>=5) return 1;i=1,j=1,a=0,b=0;
while (ChessData[a=GuangbiaoData[0]+i-1][b=GuangbiaoData[1]-i-1]==v*2&&!LogicBeOut(a,b)) i++;
while (ChessData[a=GuangbiaoData[0]-j-1][b=GuangbiaoData[1]+j-1]==v*2&&!LogicBeOut(a,b)) j++;
if (i+j-1>=5) return 1;i=1,j=1,a=0,b=0;
while (ChessData[a=GuangbiaoData[0]-i-1][b=GuangbiaoData[1]-1]==v*2&&!LogicBeOut(a,b)) i++;
while (ChessData[a=GuangbiaoData[0]+j-1][b=GuangbiaoData[1]-1]==v*2&&!LogicBeOut(a,b)) j++;
if (i+j-1>=5) return 1;i=1,j=1,a=0,b=0;
while (ChessData[a=GuangbiaoData[0]-1][b=GuangbiaoData[1]-i-1]==v*2&&!LogicBeOut(a,b)) i++;
while (ChessData[a=GuangbiaoData[0]-1][b=GuangbiaoData[1]+j-1]==v*2&&!LogicBeOut(a,b)) j++;
if (i+j-1>=5) return 1;
return 0;
}void NewShow() //新棋局的開始
{
int i,j;
for (i=0;i<15;i++)
for (j=0;j<15;j++)
ChessData[i][j]=0;
system("cls");
for (i=1;i<=29;i++)
{
for (j=1;j<=43;j++)
if(i%2==1) cout<<'-';
else if (j%3==1) cout<<'|';
else cout<<' ';
cout<<endl;
}
GuangbiaoData[0]=8;GuangbiaoData[1]=8;
MoveToEnd();
cout<<"現在請用戶1下棋 "<<endl;
cout<<"用戶1使用 w,s,a,d移動游標,空格鍵落子"<<endl;
cout<<"用戶2使用各方向鍵移動游標,回車鍵落子"<<endl;
cout<<"按下Backspace鍵悔棋,按下esc返回主菜單"<<endl;
GotoChess(8,8);
}int BeOut(int data)
{
int Xiuzheng[2],New[2];
switch (data)
{
case 1:Xiuzheng[0]=0;Xiuzheng[1]=-1 ;break;
case 2:Xiuzheng[0]=0;Xiuzheng[1]=1;break;
case 3:Xiuzheng[0]=-1;Xiuzheng[1]=0;break;
case 4:Xiuzheng[0]=1;Xiuzheng[1]=0 ;break;
case 5:Xiuzheng[0]=0;Xiuzheng[1]=0 ;break;
default:printf("BeOut函數出錯");
}
New[0]=GuangbiaoData[0]+Xiuzheng[0];
New[1]=GuangbiaoData[1]+Xiuzheng[1];
if (New[0]>15||New[0]<1||New[1]>15||New[1]<1) return 1;
else return 0;
}void UserChoose(int * choice)
{
system("cls");
printf("________________________________________________________________________________");
printf("________________________________________________________________________________");
printf(" $1.單人游戲 ");
printf(" 2.雙人游戲 ");
printf(" 3.退出遊戲 ");
printf(" 4.游戲幫助 ");
printf("________________________________________________________________________________");
printf("________________________________________________________________________________");
printf(" ");
printf(" 開心五子棋 ");
printf(" ");
printf(" 出品人:張雲聰 ");
printf(" 學號:067108034 ");
printf(" 指導老師:邵艷玲 ");
printf(" ");
printf(" ");
printf(" 溫馨提示,游戲時請把窗口最大化,以達到最佳效果 ");
printf("________________________________________________________________________________");
int temp=0,i=0;
do
{
if ((temp=getch())==224)
{
temp=getch();
if (temp==72&&i!=0)
{
gotoxy(34,3+i);
printf(" ");
i--;
gotoxy(34,3+i);
printf("$");
gotoxy(0,0);
}
else if(temp==80&&i!=3)
{
gotoxy(34,3+i);
printf(" ");
i++;
gotoxy(34,3+i);
printf("$");
gotoxy(0,0);
}
} else if (temp==13) {*choice=i+1;return;}
else if (temp==27) exit(1);
else if (temp=='1'||temp=='2'||temp=='3'||temp=='4') {*choice=temp-48;return;}}while(1);
}int CannotDo(int v1,int v2,int MoveData,int choice) //第一個輸入值為按鍵的用戶號,第二個是本應該按鍵的用戶號,第三個為按下鍵的對應值,第四個鍵代表游戲模式。
{
if (v1==3) return 0; //如果用戶輸入的為共用按鍵,則CannotDo為假else if (v1==0) return 1;//如果用戶輸入錯誤,則CannotDo為真
else if (v1!=v2&&choice==2) return 1; //如果不該此用戶輸入,而用戶進行了輸入,則CannotDo為真if (BeOut(MoveData)) return 1; //如果移動出邊界則CannotDo為真
return 0;
}int CannotLuozi() //判斷是否可以落子。
{
if (ChessData[GuangbiaoData[0]-1][GuangbiaoData[1]-1])
return 1;
else return 0;
}int luozi(int v) //玩家v落子。
{
ChessData[GuangbiaoData[0]-1][GuangbiaoData[1]-1]+=v*2;
if (v==1) cout<<'O';
else if (v==2) cout<<'X';
else cout<<"luozi函數出錯";
if (win(v)) {MoveToEnd();printf("玩家%d獲得了勝利! \n",v);for (int i=1;i<=240;i++) cout<<' ';GotoChess(GuangbiaoData[0],GuangbiaoData[1]);getch();return 1;}
MoveToEnd();
cout<<"現在請用戶"<<v%2+1<<"下棋 ";
GotoChess(GuangbiaoData[0],GuangbiaoData[1]);
return 0;
}void HuiQi(int step) //輸入的是當前的要悔的棋是第幾步
{
GuangbiaoData[0]=ChessStepData[step-1][0];
GuangbiaoData[1]=ChessStepData[step-1][1];
ChessData[GuangbiaoData[0]-1][GuangbiaoData[1]-1]=0;
GotoChess(GuangbiaoData[0],GuangbiaoData[1]);
printf("-");
MoveToEnd();
cout<<"現在請用戶"<<(step+1)%2+1<<"下棋 ";
GotoChess(GuangbiaoData[0],GuangbiaoData[1]);
}int DataGetAndChoose(int choice)
{
int MoveData=0,i=0,temp; //MoveData 0表示不可移動,1表示上,2表示下,3表示左,4表示右,5表示落子,6表示悔棋,7表示退出。
while(1)
{
loop: while (temp=Get(&MoveData),CannotDo(temp,i%2+1,MoveData,choice));
switch (MoveData)
{
case 1:
case 2:
case 3:
case 4:Move(MoveData);break;
case 7:return 0;
case 6:
if (i==0) {MoveToEnd();printf("現在無法悔棋 ");GotoChess(GuangbiaoData[0],GuangbiaoData[1]);}
else HuiQi(i--);
break;
case 5:
if (CannotLuozi()) goto loop;
if(luozi(i%2+1)) return 0;
ChessStepData[i][0]=GuangbiaoData[0];
ChessStepData[i][1]=GuangbiaoData[1];
i++;
break;
default:printf("DataGetAndChoose函數出錯");break;
}
}
return 1;
}void ShowHelp()
{
system("cls");
printf("********************************************************************************");
printf("********************************************************************************");
printf("****** 單人游戲供用戶一個人自己與自己下棋研究棋局之用 ******");
printf("****** 雙人游戲中,用戶1使用wsad控制方向,按空格落子 ******");
printf("****** 用戶2按方向鍵控制方向,回車鍵落子 ******");
printf("****** 游戲過程中按esc返回主菜單 ******");
printf("****** 游戲過程中退格鍵悔棋 ******");
printf("****** 雙人模式中某人下棋時,另一個用戶無法控制游標與落子 ******");
printf("****** ******");
printf("****** 幫助 ******");
printf("****** 按任意鍵返回 ******");
printf("********************************************************************************");
printf("********************************************************************************");
getch();
}int main()
{
int choice=0;
system("color 5b");
choose: UserChoose(&choice);
if (choice<1||choice>4) goto choose;
if (choice==3) {printf("\n謝謝您的使用,再見 "); getch();return 0;}
if (choice==4) {ShowHelp(); goto choose;}
NewShow();
DataGetAndChoose(choice);
main();
return 0;
}//這段代碼已經實現了悔棋。
Ⅳ 跪求c語言課程設計----五子棋游戲,速度啊,明天就要交了,順便也要程序設計的流程圖之類的實驗報告啊。。
- - 網上搜搜代碼應該可以,再自己改改,實驗報告都要,你當別人做服務的啊
Ⅵ 求個c語言程序設計實訓報告,,內容是關於五子棋的,,
有郵箱沒,我發給你一份
====
已經發給你了!
Ⅶ c語言課程設計報告五子棋實驗目的
熟悉掌握C語言的使用並進行初步程序設計