五子棋源碼php
Ⅰ 五子棋人機博弈游戲(cocos creator)
參考文章: 【Cocos Creator 實戰教程(1)】——人機對戰五子棋(節點事件相關)
源碼: goBang
思考一:作為對手的系統用什麼演算法下棋?
估值函數、搜索演算法和勝負判斷等
博弈演算法,在極大極小值搜索中應用alpha-beta剪枝
智能五子棋博弈程序的核心演算法
智能五子棋中的演算法研究
人機版五子棋兩種演算法概述
思考二:人機博弈的要點
1.棋局的狀態能夠在機器中表示出來,並能讓程序知道當時的博弈狀態
2.合法的走法規則如何在機器中實現,以便不讓機器隨便亂走而有失公平
3.如何讓機器從所有的合法走法中選擇最佳的走法
4.一種判斷博弈狀態優劣的方法,並能讓機器能夠做出智能的選擇
5.一個顯示博弈狀態的界面,有了這樣的界面程序才能用的起來而有意義
思考三:五子棋下棋規矩
五子棋對局,執行黑方指定開局、三手可交換、五手兩打的規定。
整個對局過程中黑方有禁手,白方無禁手。
黑方禁手有三三禁手、四四禁手和長連禁手三種
思考四:人機下棋邏輯
系統先下,黑棋落子,交換下子順序
玩家下,監測勝負(無勝負,交換下子順序)
系統下(五元組中找最優位置),監測勝負(無勝負,交換下子順序)
。。。
直到分出勝負(這里未考慮平局)
出現提示窗,告知玩家戰局結果,同時可選擇「返回菜單」或「再來一局」
具體實現:涉及知識點
官方文檔--預制資源
將其改名為Chess拖入下面assets文件夾使其成為預制資源
1.在canvas節點上掛載Menu腳本組件
2.在按鈕事件中,拖拽和選擇相應的Target,Component和Handler
初始化棋子節點斷點截圖
系統為黑棋的評分表:
找最優位置下子
個人想法
這是我學習五子棋游戲開發的記錄,後續還會寫其他游戲開發,加油!
Ⅱ 找五子棋源代碼c++
devc++運行通過,含注釋
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
#include<ctime>
using namespace std;
void gotoxy(int x,int y) {
COORD pos = {x,y};
HANDLE hOut =GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut,pos);
}//將游標移動到x,y點上
int mp[16][16]= {0},x1=0,x2=0;//地圖,用來搜索五子連成的
void print(int x) {
gotoxy(x,1);
cout<<"┬";
for(int i=2; i<=14; i++) {
gotoxy(x,i);
cout<<"┼";
}
gotoxy(x,15);
cout<<"┴";
}//輸出棋盤的中間部分
void gotoc() {
system("cls");
gotoxy(55,10);
cout<<"五 子 棋";
gotoxy(56,20);
cout<<"載入中...";
gotoxy(55,21);
cout<<"作者:北辰";
for(int j=0; j<100; j++) {
Sleep(17);
gotoxy(j+3,15);
cout<<" "<<j<<"%";
gotoxy(j,15);
cout<<"■";
}
system("cls");
for(int i=0; i<100; i++) {
for(int j=0; j<40; j++) {
gotoxy(i,j);
cout<<"■";
//SetColor(rand()%10);
}
}
system("cls");
}//載入界面函數
int main() {
gotoc();//載入
for(int i=2; i<=30; i+=2) {
gotoxy(i,0);
cout<<i/2;
}//橫坐標
for(int i=1; i<=15; i++) {
gotoxy(0,i);
cout<<i;
}//縱坐標
gotoxy(2,1);
cout<<"┌";
for(int i=2; i<=14; i++) {
gotoxy(2,i);
cout<<"├";
}
gotoxy(2,15);
cout<<"└";//輸出棋盤左側
for(int i=4; i<=28; i+=2) {
print(i);
}//用一個循環來輸出棋盤中間部分
gotoxy(30,1);
cout<<"┐";
for(int i=2; i<=14; i++) {
gotoxy(30,i);
cout<<"┤";
}
gotoxy(30,15);
cout<<"┘";//輸出棋盤右側
bool l=0;//沒什麼用的flag
long long m=2;//這個很重要,用來判斷是該白棋走還是黑棋走,每次走完++,每次判斷是偶數,該白棋,是奇數,該黑棋(一般用flag判斷,這是我個人喜好)
gotoxy(0,17);
cout<<"游戲說明:白棋先走,落子請輸入坐標,其他的不用我說了吧";//說明,一定要看
while(l=1) {
gotoxy(32,16);
int x,y;
cin>>x>>y;//讀入xy坐標
gotoxy(32,16);
cout<<" ";
if(mp[x][y]!=0) {
gotoxy(32,16);
cout<<"此位置已有落子!";
Sleep(1000);
gotoxy(32,16);
cout<<" ";
continue;
}//很重要,用來判斷此位置有沒有落子
if(x>15&&y<=15) {
gotoxy(32,16);
cout<<"x坐標超出棋盤范圍!";
Sleep(1000);
gotoxy(32,16);
cout<<" ";
continue;
}
if(y>15&&x<=15) {
gotoxy(32,16);
cout<<"y坐標超出棋盤范圍!";
Sleep(1000);
gotoxy(32,16);
cout<<" ";
continue;
}
if(y>15&&x>15) {
gotoxy(32,16);
cout<<"x和y坐標均超出棋盤范圍!";
Sleep(1000);
gotoxy(32,16);
cout<<" ";
continue;
}//以上三個if用來判斷有沒有超出棋盤大小
gotoxy(x*2,y);
if(m%2==0) {//是偶數,該白棋
cout<<"●";//輸出棋子
mp[x][y]=1;
//橫坐標搜索有沒有連成五個
if(mp[x+1][y]==1&&mp[x+2][y]==1&&mp[x+3][y]==1&&mp[x+4][y]==1) {
gotoxy(32,16);
cout<<"白棋獲勝!";
return 0;
}
if(mp[x-1][y]==1&&mp[x+1][y]==1&&mp[x+2][y]==1&&mp[x+3][y]==1) {
gotoxy(32,16);
cout<<"白棋獲勝!";
return 0;
}
if(mp[x-2][y]==1&&mp[x-1][y]==1&&mp[x+1][y]==1&&mp[x+2][y]==1) {
gotoxy(32,16);
cout<<"白棋獲勝!";
return 0;
}
if(mp[x-3][y]==1&&mp[x-2][y]==1&&mp[x-1][y]==1&&mp[x+1][y]==1) {
gotoxy(32,16);
cout<<"白棋獲勝!";
return 0;
}
if(mp[x-4][y]==1&&mp[x-3][y]==1&&mp[x-2][y]==1&&mp[x-1][y]==1) {
gotoxy(32,16);
cout<<"白棋獲勝!";
return 0;
}
//豎
if(mp[x][y+1]==1&&mp[x][y+2]==1&&mp[x][y+3]==1&&mp[x][y+4]==1) {
gotoxy(32,16);
cout<<"白棋獲勝!";
return 0;
}
if(mp[x][y-1]==1&&mp[x][y+1]==1&&mp[x][y+2]==1&&mp[x][y+3]==1) {
gotoxy(32,16);
cout<<"白棋獲勝!";
return 0;
}
if(mp[x][y-2]==1&&mp[x][y-1]==1&&mp[x][y+1]==1&&mp[x][y+2]==1) {
gotoxy(32,16);
cout<<"白棋獲勝!";
return 0;
}
if(mp[x][y-3]==1&&mp[x][y-2]==1&&mp[x][y-1]==1&&mp[x][y+1]==1) {
gotoxy(32,16);
cout<<"白棋獲勝!";
return 0;
}
if(mp[x][y-4]==1&&mp[x][y-3]==1&&mp[x][y-2]==1&&mp[x][y-1]==1) {
gotoxy(32,16);
cout<<"白棋獲勝!";
return 0;
}
//斜''
if(mp[x+1][y+1]==1&&mp[x+2][y+2]==1&&mp[x+3][y+3]==1&&mp[x+4][y+4]==1) {
gotoxy(32,16);
cout<<"白棋獲勝!";
return 0;
}
if(mp[x-1][y-1]==1&&mp[x+1][y+1]==1&&mp[x+2][y+2]==1&&mp[x+3][y+3]==1) {
gotoxy(32,16);
cout<<"白棋獲勝!";
return 0;
}
if(mp[x-2][y-2]==1&&mp[x-1][y-1]==1&&mp[x+1][y+1]==1&&mp[x+2][y+2]==1) {
gotoxy(32,16);
cout<<"白棋獲勝!";
return 0;
}
if(mp[x-3][y-3]==1&&mp[x-2][y-2]==1&&mp[x-1][y-1]==1&&mp[x+1][y+1]==1) {
gotoxy(32,16);
cout<<"白棋獲勝!";
return 0;
}
if(mp[x-4][y-4]==1&&mp[x-3][y-3]==1&&mp[x-2][y-2]==1&&mp[x-1][y-1]==1) {
gotoxy(32,16);
cout<<"白棋獲勝!";
return 0;
}
//斜'/'
if(mp[x-1][y+1]==1&&mp[x-2][y+2]==1&&mp[x-3][y+3]==1&&mp[x-4][y+4]==1) {
gotoxy(32,16);
cout<<"白棋獲勝!";
return 0;
}
if(mp[x+1][y-1]==1&&mp[x-1][y+1]==1&&mp[x-2][y+2]==1&&mp[x-3][y+3]==1) {
gotoxy(32,16);
cout<<"白棋獲勝!";
return 0;
}
if(mp[x+2][y-2]==1&&mp[x+1][y-1]==1&&mp[x-1][y+1]==1&&mp[x-2][y+2]==1) {
gotoxy(32,16);
cout<<"白棋獲勝!";
return 0;
}
if(mp[x+3][y-3]==1&&mp[x+2][y-2]==1&&mp[x+1][y-1]==1&&mp[x-1][y+1]==1) {
gotoxy(32,16);
cout<<"白棋獲勝!";
return 0;
}
if(mp[x+4][y-4]==1&&mp[x+3][y-3]==1&&mp[x+2][y-2]==1&&mp[x+1][y-1]==1) {
gotoxy(32,16);
cout<<"白棋獲勝!";
return 0;
}
} else if(m%2==1) {//為奇數,該黑棋
cout<<"○";
mp[x][y]=2;
//橫
if(mp[x+1][y]==2&&mp[x+2][y]==2&&mp[x+3][y]==2&&mp[x+4][y]==2) {
gotoxy(32,16);
cout<<"黑棋獲勝!";
return 0;
}
if(mp[x-1][y]==2&&mp[x+1][y]==2&&mp[x+2][y]==2&&mp[x+3][y]==2) {
gotoxy(32,16);
cout<<"黑棋獲勝!";
return 0;
}
if(mp[x-2][y]==2&&mp[x-1][y]==2&&mp[x+1][y]==2&&mp[x+2][y]==2) {
gotoxy(32,16);
cout<<"黑棋獲勝!";
return 0;
}
if(mp[x-3][y]==2&&mp[x-2][y]==2&&mp[x-1][y]==2&&mp[x+1][y]==2) {
gotoxy(32,16);
cout<<"黑棋獲勝!";
return 0;
}
if(mp[x-4][y]==2&&mp[x-3][y]==2&&mp[x-2][y]==2&&mp[x-1][y]==2) {
gotoxy(32,16);
cout<<"黑棋獲勝!";
return 0;
}
//豎
if(mp[x][y+1]==2&&mp[x][y+2]==2&&mp[x][y+3]==2&&mp[x][y+4]==2) {
gotoxy(32,16);
cout<<"黑棋獲勝!";
return 0;
}
if(mp[x][y-1]==2&&mp[x][y+1]==2&&mp[x][y+2]==2&&mp[x][y+3]==2) {
gotoxy(32,16);
cout<<"黑棋獲勝!";
return 0;
}
if(mp[x][y-2]==2&&mp[x][y-1]==2&&mp[x][y+1]==2&&mp[x][y+2]==2) {
gotoxy(32,16);
cout<<"黑棋獲勝!";
return 0;
}
if(mp[x][y-3]==2&&mp[x][y-2]==2&&mp[x][y-1]==2&&mp[x][y+1]==2) {
gotoxy(32,16);
cout<<"黑棋獲勝!";
return 0;
}
if(mp[x][y-4]==2&&mp[x][y-3]==2&&mp[x][y-2]==2&&mp[x][y-1]==2) {
gotoxy(32,16);
cout<<"黑棋獲勝!";
return 0;
}
//斜''
if(mp[x+1][y+1]==2&&mp[x+2][y+2]==2&&mp[x+3][y+3]==2&&mp[x+4][y+4]==2) {
gotoxy(32,16);
cout<<"黑棋獲勝!";
return 0;
}
if(mp[x-1][y-1]==2&&mp[x+1][y+1]==2&&mp[x+2][y+2]==2&&mp[x+3][y+3]==2) {
gotoxy(32,16);
cout<<"黑棋獲勝!";
return 0;
}
if(mp[x-2][y-2]==2&&mp[x-1][y-1]==2&&mp[x+1][y+1]==2&&mp[x+2][y+2]==2) {
gotoxy(32,16);
cout<<"黑棋獲勝!";
return 0;
}
if(mp[x-3][y-3]==2&&mp[x-2][y-2]==2&&mp[x-1][y-1]==2&&mp[x+1][y+1]==2) {
gotoxy(32,16);
cout<<"黑棋獲勝!";
return 0;
}
if(mp[x-4][y-4]==2&&mp[x-3][y-3]==2&&mp[x-2][y-2]==2&&mp[x-1][y-1]==2) {
gotoxy(32,16);
cout<<"黑棋獲勝!";
return 0;
}
//斜'/'
if(mp[x-1][y+1]==2&&mp[x-2][y+2]==2&&mp[x-3][y+3]==2&&mp[x-4][y+4]==2) {
gotoxy(32,16);
cout<<"黑棋獲勝!";
return 0;
}
if(mp[x+1][y-1]==2&&mp[x-1][y+1]==2&&mp[x-2][y+2]==2&&mp[x-3][y+3]==2) {
gotoxy(32,16);
cout<<"黑棋獲勝!";
return 0;
}
if(mp[x+2][y-2]==2&&mp[x+1][y-1]==2&&mp[x-1][y+1]==2&&mp[x-2][y+2]==2) {
gotoxy(32,16);
cout<<"黑棋獲勝!";
return 0;
}
if(mp[x+3][y-3]==2&&mp[x+2][y-2]==2&&mp[x+1][y-1]==2&&mp[x-1][y+1]==2) {
gotoxy(32,16);
cout<<"黑棋獲勝!";
return 0;
}
if(mp[x+4][y-4]==2&&mp[x+3][y-3]==2&&mp[x+2][y-2]==2&&mp[x+1][y-1]==2) {
gotoxy(32,16);
cout<<"黑棋獲勝!";
return 0;
}
}
m++;//不要忘記++m
}
return 0;//這個沒什麼用了,不過比賽時不要忘記加哦,否則判0分
}
Ⅲ 五子棋源代碼html
js代碼:
定義canvas及黑白棋變數
<font color="#2f4f4f" face="微軟雅黑" size="3">var canvas;
var context;
var isWhite = true;//設置是否該輪到白棋
var isWell = false;//設置該局棋盤是否贏了,如果贏了就不能再走了
var img_b = new Image();
img_b.src = "images/b.png";//白棋圖片
var img_w = new Image();
img_w.src = "images/c.png";//黑棋圖片</font>
為棋盤的二維數組用來保存棋盤信息
<font color="#2f4f4f" face="微軟雅黑" size="3"> var chessData = new Array(15);//初始化0為沒有走過的,1為白棋走的,2為黑棋走的
for (var x = 0; x < 15; x++) {
chessData[x] = new Array(15);
for (var y = 0; y < 15; y++) {
chessData[x][y] = 0;
}
}</font>
繪制棋盤的線
<font color="#2f4f4f" face="微軟雅黑" size="3"> for (var i = 0; i <= 640; i += 40) {
context.beginPath();
context.moveTo(0, i);
context.lineTo(640, i);
context.closePath();
context.stroke();
context.beginPath();
context.moveTo(i, 0);
context.lineTo(i, 640);
context.closePath();
context.stroke();
}
}</font>
判斷該棋局的輸贏
<font color="#2f4f4f" face="微軟雅黑" size="3"> if (count1 >= 5 || count2 >= 5 || count3 >= 5 || count4 >= 5) {
if (chess == 1) {
alert("白棋贏了");
}
else {
alert("黑棋贏了");
}
isWell = true;//設置該局棋盤已經贏了,不可以再走了
}</font>
html代碼:
<font color="#2f4f4f" face="微軟雅黑" size="3"><body onload="drawRect()">
<div>
<canvas width="640" id="canvas" onmousedown="play(event)" height="640">你的瀏覽器不支持HTML5 canvas ,請使用 google chrome 瀏覽器 打開.
</canvas>
</div>
</body></font>
Ⅳ 用C++編寫的小游戲源代碼
五子棋的代碼:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
usingnamespacestd;
constintN=15;//15*15的棋盤
constcharChessBoardflag='';//棋盤標志
constcharflag1='o';//玩家1或電腦的棋子標志
constcharflag2='X';//玩家2的棋子標志
typedefstructCoordinate//坐標類
{
intx;//代錶行
inty;//代表列
}Coordinate;
classGoBang//五子棋類
{
public:
GoBang()//初始化
{
InitChessBoard();
}
voidPlay()//下棋
{
CoordinatePos1;//玩家1或電腦
CoordinatePos2;//玩家2
intn=0;
while(1)
{
intmode=ChoiceMode();
while(1)
{
if(mode==1)//電腦vs玩家
{
ComputerChess(Pos1,flag1);//電腦下棋
if(GetVictory(Pos1,0,flag1)==1)//0表示電腦,真表示獲勝
break;
PlayChess(Pos2,2,flag2);//玩家2下棋
if(GetVictory(Pos2,2,flag2))//2表示玩家2
break;
}
else//玩家1vs玩家2
{
PlayChess(Pos1,1,flag1);//玩家1下棋
if(GetVictory(Pos1,1,flag1))//1表示玩家1
break;
PlayChess(Pos2,2,flag2);//玩家2下棋
if(GetVictory(Pos2,2,flag2))//2表示玩家2
break;
}
}
cout<<"***再來一局***"<<endl;
cout<<"yorn:";
charc='y';
cin>>c;
if(c=='n')
break;
}
}
protected:
intChoiceMode()//選擇模式
{
inti=0;
system("cls");//系統調用,清屏
InitChessBoard();//重新初始化棋盤
cout<<"***0、退出1、電腦vs玩家2、玩家vs玩家***"<<endl;
while(1)
{
cout<<"請選擇:";
cin>>i;
if(i==0)//選擇0退出
exit(1);
if(i==1||i==2)
returni;
cout<<"輸入不合法"<<endl;
}
}
voidInitChessBoard()//初始化棋盤
{
for(inti=0;i<N+1;++i)
{
for(intj=0;j<N+1;++j)
{
_ChessBoard[i][j]=ChessBoardflag;
}
}
}
voidPrintChessBoard()//列印棋盤,這個函數可以自己調整
{
system("cls");//系統調用,清空屏幕
for(inti=0;i<N+1;++i)
{
for(intj=0;j<N+1;++j)
{
if(i==0)//列印列數字
{
if(j!=0)
printf("%d",j);
else
printf("");
}
elseif(j==0)//列印行數字
printf("%2d",i);
else
{
if(i<N+1)
{
printf("%c|",_ChessBoard[i][j]);
}
}
}
cout<<endl;
cout<<"";
for(intm=0;m<N;m++)
{
printf("--|");
}
cout<<endl;
}
}
voidPlayChess(Coordinate&pos,intplayer,intflag)//玩家下棋
{
PrintChessBoard();//列印棋盤
while(1)
{
printf("玩家%d輸入坐標:",player);
cin>>pos.x>>pos.y;
if(JudgeValue(pos)==1)//坐標合法
break;
cout<<"坐標不合法,重新輸入"<<endl;
}
_ChessBoard[pos.x][pos.y]=flag;
}
voidComputerChess(Coordinate&pos,charflag)//電腦下棋
{
PrintChessBoard();//列印棋盤
intx=0;
inty=0;
while(1)
{
x=(rand()%N)+1;//產生1~N的隨機數
srand((unsignedint)time(NULL));
y=(rand()%N)+1;//產生1~N的隨機數
srand((unsignedint)time(NULL));
if(_ChessBoard[x][y]==ChessBoardflag)//如果這個位置是空的,也就是沒有棋子
break;
}
pos.x=x;
pos.y=y;
_ChessBoard[pos.x][pos.y]=flag;
}
intJudgeValue(constCoordinate&pos)//判斷輸入坐標是不是合法
{
if(pos.x>0&&pos.x<=N&&pos.y>0&&pos.y<=N)
{
if(_ChessBoard[pos.x][pos.y]==ChessBoardflag)
{
return1;//合法
}
}
return0;//非法
}
intJudgeVictory(Coordinatepos,charflag)//判斷有沒有人勝負(底層判斷)
{
intbegin=0;
intend=0;
intbegin1=0;
intend1=0;
//判斷行是否滿足條件
(pos.y-4)>0?begin=(pos.y-4):begin=1;
(pos.y+4)>N?end=N:end=(pos.y+4);
for(inti=pos.x,j=begin;j+4<=end;j++)
{
if(_ChessBoard[i][j]==flag&&_ChessBoard[i][j+1]==flag&&
_ChessBoard[i][j+2]==flag&&_ChessBoard[i][j+3]==flag&&
_ChessBoard[i][j+4]==flag)
return1;
}
//判斷列是否滿足條件
(pos.x-4)>0?begin=(pos.x-4):begin=1;
(pos.x+4)>N?end=N:end=(pos.x+4);
for(intj=pos.y,i=begin;i+4<=end;i++)
{
if(_ChessBoard[i][j]==flag&&_ChessBoard[i+1][j]==flag&&
_ChessBoard[i+2][j]==flag&&_ChessBoard[i+3][j]==flag&&
_ChessBoard[i+4][j]==flag)
return1;
}
intlen=0;
//判斷主對角線是否滿足條件
pos.x>pos.y?len=pos.y-1:len=pos.x-1;
if(len>4)
len=4;
begin=pos.x-len;//橫坐標的起始位置
begin1=pos.y-len;//縱坐標的起始位置
pos.x>pos.y?len=(N-pos.x):len=(N-pos.y);
if(len>4)
len=4;
end=pos.x+len;//橫坐標的結束位置
end1=pos.y+len;//縱坐標的結束位置
for(inti=begin,j=begin1;(i+4<=end)&&(j+4<=end1);++i,++j)
{
if(_ChessBoard[i][j]==flag&&_ChessBoard[i+1][j+1]==flag&&
_ChessBoard[i+2][j+2]==flag&&_ChessBoard[i+3][j+3]==flag&&
_ChessBoard[i+4][j+4]==flag)
return1;
}
//判斷副對角線是否滿足條件
(pos.x-1)>(N-pos.y)?len=(N-pos.y):len=pos.x-1;
if(len>4)
len=4;
begin=pos.x-len;//橫坐標的起始位置
begin1=pos.y+len;//縱坐標的起始位置
(N-pos.x)>(pos.y-1)?len=(pos.y-1):len=(N-pos.x);
if(len>4)
len=4;
end=pos.x+len;//橫坐標的結束位置
end1=pos.y-len;//縱坐標的結束位置
for(inti=begin,j=begin1;(i+4<=end)&&(j-4>=end1);++i,--j)
{
if(_ChessBoard[i][j]==flag&&_ChessBoard[i+1][j-1]==flag&&
_ChessBoard[i+2][j-2]==flag&&_ChessBoard[i+3][j-3]==flag&&
_ChessBoard[i+4][j-4]==flag)
return1;
}
for(inti=1;i<N+1;++i)//棋盤有沒有下滿
{
for(intj=1;j<N+1;++j)
{
if(_ChessBoard[i][j]==ChessBoardflag)
return0;//0表示棋盤沒滿
}
}
return-1;//和棋
}
boolGetVictory(Coordinate&pos,intplayer,intflag)//對JudgeVictory的一層封裝,得到具體那個玩家獲勝
{
intn=JudgeVictory(pos,flag);//判斷有沒有人獲勝
if(n!=0)//有人獲勝,0表示沒有人獲勝
{
PrintChessBoard();
if(n==1)//有玩家贏棋
{
if(player==0)//0表示電腦獲勝,1表示玩家1,2表示玩家2
printf("***電腦獲勝*** ");
else
printf("***恭喜玩家%d獲勝*** ",player);
}
else
printf("***雙方和棋*** ");
returntrue;//已經有人獲勝
}
returnfalse;//沒有人獲勝
}
private:
char_ChessBoard[N+1][N+1];
};
(4)五子棋源碼php擴展閱讀:
設計思路
1、進行問題分析與設計,計劃實現的功能為,開局選擇人機或雙人對戰,確定之後比賽開始。
2、比賽結束後初始化棋盤,詢問是否繼續比賽或退出,後續可加入復盤、悔棋等功能。
3、整個過程中,涉及到了棋子和棋盤兩種對象,同時要加上人機對弈時的AI對象,即涉及到三個對象。
Ⅳ 老師讓我做一個智能游戲 五子棋 哪位大哥給個源碼(有說明) java C++都好
我寫過一個C#對弈版本的
可惜幫不上什麼忙
我就是覺得寫人機太復雜了,才寫對弈的
你去多看看人工智慧的資料吧,
哪些算此攜法沒一個好理解的森兄伏!
不塵蠢然寫出來的東西就太笨了
祝你成功
Ⅵ c語言五子棋代碼,
package day17.gobang;
import java.util.Arrays;
public class GoBangGame {
public static final char BLANK='*';
public static final char BLACK='@';
public static final char WHITE='O';
public static final int MAX = 16;
private static final int COUNT = 5;
//棋盤
private char[][] board;
public GoBangGame() {
}
//開始游戲
public void start() {
board = new char[MAX][MAX];
//把二維數組都填充『*』
for(char[] ary: board){
Arrays.fill(ary, BLANK);
}
}
public char[][] getChessBoard(){
return board;
}
public void addBlack(int x, int y) throws ChessExistException{
//@
//char blank = '*';
//System.out.println( x +"," + y + ":" + board[y][x] + "," + BLANK);
if(board[y][x] == BLANK){// x, y 位置上必須是空的才可以添棋子
board[y][x] = BLACK;
return;
}
throw new ChessExistException("已經有棋子了!");
}
public void addWhite(int x, int y)
throws ChessExistException{
if(board[y][x] == BLANK){// x, y 位置上必須是空的才可以添棋子
board[y][x] = WHITE;
return;
}
throw new ChessExistException("已經有棋子了!");
}
//chess 棋子:'@'/'O'
public boolean winOnY(char chess, int x, int y){
//先找到y方向第一個不是 blank的棋子
int top = y;
while(true){
if(y==0 || board[y-1][x]!=chess){
//如果y已經是棋盤的邊緣, 或者的前一個不是chess
//就不再繼續查找了
break;
}
y--;
top = y;
}
//向回統計所有chess的個數,如果是COUNT個就贏了
int count = 0;
y = top;
while(true){
if(y==MAX || board[y][x]!=chess){
//如果找到頭 或者 下一個子不是chess 就不再繼續統計了
break;
}
count++;
y++;
}
return count==COUNT;
}
//chess 棋子:'@'/'O'
public boolean winOnX(char chess, int x, int y){
//先找到x方向第一個不是 blank的棋子
int top = x;
while(true){
if(x==0 || board[y][x-1]!=chess){
//如果x已經是棋盤的邊緣, 或者的前一個不是chess
//就不再繼續查找了
break;
}
x--;
top = x;
}
//向回統計所有chess的個數,如果是COUNT個就贏了
int count = 0;
x = top;
while(true){
if(x==MAX || board[y][x]!=chess){
//如果找到頭 或者 下一個子不是chess 就不再繼續統計了
break;
}
count++;
x++;
}
return count==COUNT;
}
//chess 棋子:'@'/'O'
public boolean winOnXY(char chess, int x, int y){
//先找MAX向第一個不是 blank的棋子
int top = y;
int left = x;
while(true){
if(x==0 || y==0 || board[y-1][x-1]!=chess){
//如果x已經是棋盤的邊緣, 或者的前一個不是chess
//就不再繼續查找了
break;
}
x--;
y--;
top = y;
left=x;
}
//向回統計所有chess的個數,如果是COUNT個就贏了
int count = 0;
x = left;
y = top;
while(true){
if(x==MAX || y==MAX || board[y][x]!=chess){
//如果找到頭 或者 下一個子不是chess 就不再繼續統計了
break;
}
count++;
x++;
y++;
}
return count==COUNT;
}
//chess 棋子:'@'/'O'
public boolean winOnYX(char chess, int x, int y){
//先找到x方向第一個不是 blank的棋子
int top = y;
int left = x;
while(true){
if(x==MAX-1 || y==0 || board[y-1][x+1]!=chess){
//如果x已經是棋盤的邊緣, 或者的前一個不是chess
//就不再繼續查找了
break;
}
x++;
y--;
top = y;
left=x;
}
//向回統計所有chess的個數,如果是COUNT個就贏了
int count = 0;
x = left;
y = top;
while(true){
if(x==0 || y==MAX || board[y][x]!=chess){
//如果找到頭 或者 下一個子不是chess 就不再繼續統計了
break;
}
count++;
x--;
y++;
}
return count==COUNT;
}
public boolean whiteIsWin(int x, int y) {
//在任何一個方向上贏了,都算贏
return winOnY(WHITE, x, y) ||
winOnX(WHITE, x, y) ||
winOnXY(WHITE, x, y) ||
winOnYX(WHITE, x, y);
}
public boolean blackIsWin(int x, int y) {
return winOnY(BLACK, x, y) ||
winOnX(BLACK, x, y) ||
winOnXY(BLACK, x, y) ||
winOnYX(BLACK, x, y);
}
}
Ⅶ 求一個c++實現人機對戰,人人對戰的五子棋游戲源代碼,急用,謝謝
五子棋範例的源程序:目錄renju下的內容
程序在附件中,需要請免費下載
renju.dsw
renju.dsp
這兩個是項目文件。包含整個項目的文件配置等信息
RESOURCE.H
renju.rc
這是整個工程中使用的Windows資源列表。包括置於res子目錄下的圖標,
點陣圖以及游標等內容。
Renju.h
這是應用程序的主頭文件。包含了通用於工程的其他頭文件。以及
CRenjuApp類的聲明。
renju.cpp
這是應用程序的主源程序。包含整個程序的入口點。CRenjuApp類的實現。
StdAfx.h
StdAfx.cpp
這對文件由用於將一些預編譯信息納入程序。編譯後將產生stdafx.obj
define.h
這是一個包含程序中的數據表示的定義的頭文件。
NewGame.h
NewGame.cpp
這一對文件定義並實現用於新游戲的設置的對話框。
renjuDlg.h
renjuDlg.cpp
這一對文件定義並實現了,五子棋的主界面。
Eveluation.h
Eveluation.cpp
這一對文件定義並實現了估值核心類。
MoveGenerator.h
MoveGenerator.cpp
這一對文件定義並實現了走法產生器。
SearchEngine.h
SearchEngine.cpp
這一對文件定義了搜索引擎介面。
HistoryHeuristic.h
HistoryHeuristic.cpp
這一對文件定義並實現歷史啟發類。
TranspositionTable.h
TranspositionTable.cpp
這一對文件定義並實現置換表類。
NegaScout_TT_HH.h
NegaScout_TT_HH.cpp
這一對文件定義並實現歷史啟發和置換表增強的NegaScout搜索引擎。
Directory of renju es
chess.rc2//資源文件
chess.ico//圖標文件
若滿意請及時採納,謝謝
Ⅷ 求五子棋判別輸贏的源代碼。棋盤19*19的最好,用請各位高手不吝賜教,多謝!
恰好有一個:
#include "stdafx.h"
#include <stdio.h>
#include <math.h>
//�辦跑計��
HINSTANCE hInst;
HBITMAP chess[2];
HDC hdc,mdc,bufdc;
HWND hWnd;
DWORD tPre,tNow;
int board[10][10];
bool ptab[10][10][192];
bool ctab[10][10][192];
int win[2][192];
int num[2];
bool turn,over;
int winner;
//ㄧΑ��
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void MyPaint(HDC hdc);
void InitGame();
void ComTurn();
//猜敗****�祘Α**************************************
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
MyRegisterClass(hInstance);
//磅《�﹍て穗森顫ㄧΑ
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
//笴欄癹伴
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
tNow = GetTickCount();
if(tNow-tPre >= 100)
MyPaint(hdc);
}
}
return msg.wParam;
}
//****)竡の爹�跌怠摸春旅�ㄧΑ*************************
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "canvas";
wcex.hIconSm = NULL;
return RegisterClassEx(&wcex);
}
//****�﹍ㄧΑ*************************************
// 囪弦�鋇�の㊣� InitGame() ㄧΑ秨﹍囪Ы
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HBITMAP tile,bmp;
int rowNum,colNum;
int i,x,y;
hInst = hInstance;
hWnd = CreateWindow("canvas", "酶瓜跌怠" , WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
MoveWindow(hWnd,10,10,480,520,true);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
hdc = GetDC(hWnd);
mdc = CreateCompatibleDC(hdc);
bufdc = CreateCompatibleDC(hdc);
bmp = CreateCompatibleBitmap(hdc,450,450);
SelectObject(mdc,bmp);
tile = (HBITMAP)LoadImage(NULL,"tile.bmp",IMAGE_BITMAP,45,45,LR_LOADFROMFILE);
chess[0] = (HBITMAP)LoadImage(NULL,"chess0.bmp",IMAGE_BITMAP,38,38,LR_LOADFROMFILE);
chess[1] = (HBITMAP)LoadImage(NULL,"chess1.bmp",IMAGE_BITMAP,38,38,LR_LOADFROMFILE);
for (i=0;i<100;i++)
{
rowNum = i / 10;
colNum = i % 10;
x = colNum * 45;
y = rowNum * 45;
SelectObject(bufdc,tile);
BitBlt(mdc,x,y,45,45,bufdc,0,0,SRCCOPY);
}
InitGame();
MyPaint(hdc);
return TRUE;
}
//****囪Ы�﹍ㄧΑ***********************************
// 1.砞)囪弦�﹍�篈の莉秤�ず甧
// 2.∕)�����よ
void InitGame()
{
int i,j,k;
int count=0;
over = false;
num[0] = num[1] = 0;
//砞)�產籔筿福���莉秤艙�い�囪�計
for(i=0;i<192;i++)
{
win[0][i] = 0;
win[1][i] = 0;
}
//�﹍て囪弦�篈
for(i=0;i<10;i++)
for(j=0;j<10;j++)
board[i][j] = 2;
//砞)�キよ��莉秤艙�
for(i=0;i<10;i++)
for(j=0;j<6;j++)
{
for(k=0;k<5;k++)
{
ptab[i][j+k][count] = true;
ctab[i][j+k][count] = true;
}
count++;
}
//砞)��よ��莉秤艙�
for(i=0;i<10;i++)
for(j=0;j<6;j++)
{
for(k=0;k<5;k++)
{
ptab[j+k][i][count] = true;
ctab[j+k][i][count] = true;
}
count++;
}
//砞)タ癸à絬よ��莉秤艙�
for(i=0;i<6;i++)
for(j=0;j<6;j++)
{
for(k=0;k<5;k++)
{
ptab[j+k][i+k][count] = true;
ctab[j+k][i+k][count] = true;
}
count++;
}
//砞)は癸à絬よ��莉秤艙�
for(i=0;i<6;i++)
for(j=9;j>=4;j--)
{
for(k=0;k<5;k++)
{
ptab[j-k][i+k][count] = true;
ctab[j-k][i+k][count] = true;
}
count++;
}
//睹計∕)パê�よ��囪�
srand(GetTickCount());
if(rand()%2 == 0)
turn = true;
else
turn = false;
}
//****筿福��ㄧΑ***********************************
// 1.璸衡莉秤だ計
// 2.匡拒程ㄎ�竚秈《��笆�
void ComTurn()
{
int grades[2][10][10];
int m,n,i,max=0;
int u,v;
for(m=0;m<10;m++)
for(n=0;n<10;n++)
{
grades[0][m][n] = 0;
grades[1][m][n] = 0;
if(board[m][n] == 2)
{
for(i=0;i<192;i++)
{
//璸衡�產��囪���莉秤だ計
if(ptab[m][n][i] && win[0][i] != 7)
{
switch(win[0][i])
{
case 0:
grades[0][m][n]+=1;
break;
case 1:
grades[0][m][n]+=200;
break;
case 2:
grades[0][m][n]+=400;
break;
case 3:
grades[0][m][n]+=2000;
break;
case 4:
grades[0][m][n]+=10000;
break;
}
}
//璸衡筿福��囪���莉秤だ計
if(ctab[m][n][i] && win[1][i] != 7)
{
switch(win[1][i])
{
case 0:
grades[1][m][n]+=1;
break;
case 1:
grades[1][m][n]+=220;
break;
case 2:
grades[1][m][n]+=420;
break;
case 3:
grades[1][m][n]+=2100;
break;
case 4:
grades[1][m][n]+=20000;
break;
}
}
}
if(max == 0)
{
u = m;
v = n;
}
if(grades[0][m][n] > max)
{
max = grades[0][m][n];
u = m;
v = n;
}
else if(grades[0][m][n] == max)
{
if(grades[1][m][n] > grades[1][u][v])
{
u = m;
v = n;
}
}
if(grades[1][m][n] > max)
{
max = grades[1][m][n];
u = m;
v = n;
}
else if(grades[1][m][n] == max)
{
if(grades[0][m][n] > grades[0][u][v])
{
u = m;
v = n;
}
}
}
}
board[u][v] = 1; //砞)�筿福�囪�
num[1]++;
if(num[0] == 50 && num[1] == 50)
{
winner = 2; //キも
over = true;
}
else
for(i=0;i<192;i++)
{
if(ctab[u][v][i])
{
win[1][i]++;
ptab[u][v][i] = false;
win[0][i] = 7;
if(win[1][i] == 5)
{
winner = 1;
over = true;
}
}
}
turn = true; //傳�產�
}
//****�璹酶瓜ㄧΑ*********************************
// 跌怠禟瓜の陪ボ癟�
void MyPaint(HDC hdc)
{
int m,n;
char *str;
if(over)
{
switch(winner)
{
case 0:
str = "眤墓� ! �� F1 ��穝秈《笴欄..";
break;
case 1:
str = "筿福墓� ! �� F1 ��穝秈《笴欄..";
break;
case 2:
str = "ぃだ秤璽 ! �� F1 ��穝秈《笴欄..";
break;
}
TextOut(hdc,10,470,str,strlen(str));
}
else if(!turn) //筿福��
{
str = "筿福�σい... ";
TextOut(hdc,10,470,str,strlen(str));
ComTurn();
}
else
{
str = "贛眤��... ";
TextOut(hdc,10,470,str,strlen(str));
}
for(m=0;m<10;m++)
for(n=0;n<10;n++)
{
if(board[m][n] == 0) //禟��產囪�
{
SelectObject(bufdc,chess[0]);
BitBlt(mdc,m*45+3,n*45+3,38,38,bufdc,0,0,SRCCOPY);
}
else if(board[m][n] == 1) //禟�筿福囪�
{
SelectObject(bufdc,chess[1]);
BitBlt(mdc,m*45+3,n*45+3,38,38,bufdc,0,0,SRCCOPY);
}
else //禟���
{
SelectObject(bufdc,chess[1]);
BitBlt(mdc,m*45+3,n*45+3,38,38,bufdc,0,0,WHITENESS);
}
}
BitBlt(hdc,10,10,450,450,mdc,0,0,SRCCOPY);
tPre = GetTickCount();
}
//****癟�矪瞶ㄧΑ***********************************
// 1.砞)�� F1 齡�穝秨﹍笴欄
// 2.矪瞶�產��菲公オ齡�囪��笆�
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int x,y,m,n,i;
switch (message)
{
case WM_KEYDOWN: //���齡癟�
switch (wParam)
{
case VK_ESCAPE: //�� Esc 齡
PostQuitMessage( 0 );
break;
case VK_F1: //�� F1 齡
InitGame();
break;
}
case WM_LBUTTONDOWN: //��菲公オ齡癟�
if(!over)
if(turn)
{
x = LOWORD(lParam); //�眔菲公 X 畒夾
y = HIWORD(lParam); //�眔菲公 Y 畒夾
if(x > 10 && x < 460 && y> 10 && y < 460)
{
m = (int)floor((x-10)/45);
n = (int)floor((y-10)/45);
if(board[m][n] == 2)
{
board[m][n] = 0; //砞)��產�囪�
num[0]++;
if(num[0] == 50 && num[1] == 50)
{
winner = 2; //キも
over = true;
}
else
for(i=0;i<192;i++)
{
if(ptab[m][n][i])
{
win[0][i]++;
ctab[m][n][i] = false;
win[1][i] = 7;
if(win[0][i] == 5)
{
winner = 0;
over = true;
}
}
}
turn = false; //傳筿福�
}
}
}
break;
case WM_DESTROY: //跌怠擋�癟�
DeleteDC(mdc);
DeleteDC(bufdc);
DeleteObject(chess[0]);
DeleteObject(chess[1]);
ReleaseDC(hWnd,hdc);
PostQuitMessage(0);
break;
default: //ㄤウ癟�
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
Ⅸ 在線等!求一個python 五子棋源代碼,最好是有「人人對弈」和「人機對弈」功能的,不勝感謝!
試試這個吧。
import numpy as np
import pygame
import sys
import traceback
import
from pygame.locals import *
pygame.init()
pygame.mixer.init()
#顏色
background=(201,202,187)
checkerboard=(80,80,80)
button=(52,53,44)
#音樂
play_chess_sound = pygame.mixer.Sound("music/play_chess.wav")
play_chess_sound.set_volume(0.2)
button_sound = pygame.mixer.Sound("music/button.wav")
button_sound.set_volume(0.2)
victor_sound = pygame.mixer.Sound("music/victory.wav")
victor_sound.set_volume(0.2)
#繪制棋盤
def Draw_a_chessboard(screen):
#填充背景色
screen.fill(background)
Background=pygame.image.load("background.jpg").convert_alpha()
screen.blit(Background,(0,0))
#畫棋盤
for i in range(21):
pygame.draw.line(screen, checkerboard, (40*i+3, 3), (40*i+3, 803))
pygame.draw.line(screen, checkerboard, (3, 40*i+3), (803, 40*i+3))
#畫邊線
pygame.draw.line(screen, checkerboard, (3, 3), (803, 3),5)
pygame.draw.line(screen, checkerboard, (3, 3), (3, 803),5)
pygame.draw.line(screen, checkerboard, (803, 3), (803, 803),5)
pygame.draw.line(screen, checkerboard, (3, 803), (803, 803),5)
#畫定位點
pygame.draw.circle(screen, checkerboard, (163, 163), 6)
pygame.draw.circle(screen, checkerboard, (163, 643), 6)
pygame.draw.circle(screen, checkerboard, (643, 163), 6)
pygame.draw.circle(screen, checkerboard, (643, 643), 6)
pygame.draw.circle(screen, checkerboard, (403, 403), 6)
#畫『悔棋』『重新開始』跟『退出』按鈕
pygame.draw.rect(screen,button,[900,350,120,100],5)
pygame.draw.rect(screen,button,[900,500,200,100],5)
pygame.draw.rect(screen,button,[900,650,200,100],5)
s_font=pygame.font.Font('font.ttf',40)
text1=s_font.render("悔棋",True,button)
text2=s_font.render("重新開始",True,button)
text3=s_font.render("退出遊戲",True,button)
screen.blit(text1,(920,370))
screen.blit(text2,(920,520))
screen.blit(text3,(920,670))
#繪制棋子(橫坐標,縱坐標,屏幕,棋子顏色(1代表黑,2代表白))
def Draw_a_chessman(x,y,screen,color):
if color==1:
Black_chess=pygame.image.load("Black_chess.png").convert_alpha()
screen.blit(Black_chess,(40*x+3-15,40*y+3-15))
if color==2:
White_chess=pygame.image.load("White_chess.png").convert_alpha()
screen.blit(White_chess,(40*x+3-15,40*y+3-15))
#繪制帶有棋子的棋盤
def Draw_a_chessboard_with_chessman(map,screen):
screen.fill(background)
Draw_a_chessboard(screen)
for i in range(24):
for j in range(24):
Draw_a_chessman(i+1,j+1,screen,map[i][j])
#定義存儲棋盤的列表,
#列表為24列24行是因為判斷是否勝利函數里的索引會超出19
#列表大一點不會對游戲有什麼影響
map=[]
for i in range(24):
map.append([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
#清零map列表
def clear():
global map
for i in range(24):
for j in range(24):
map[i][j]=0
#判斷是否勝利
def win(i, j):
k = map[i][j]
p=[]
for a in range(20):
p.append(0)
for i3 in range(i-4,i+5):
for j3 in range(j-4,j+5):
if (map[i3][j3] == k and i3 - i == j3 - j and i3 <= i and j3 <= j):
p[0]+=1
if (map[i3][j3] == k and j3 == j and i3 <= i and j3 <= j):
p[1]+=1
if (map[i3][j3] == k and i3 == i and i3 <= i and j3 <= j):
p[2]+=1
if (map[i3][j3] == k and i3 - i == j3 - j and i3 >= i and j3 >= j):
p[3]+=1
if (map[i3][j3] == k and j3 == j and i3 >= i and j3 >= j):
p[4]+=1
if (map[i3][j3] == k and i3 == i and i3 >= i and j3 >= j):
p[5]+=1
if (map[i3][j3] == k and i - i3 == j3 - j and i3 <= i and j3 >= j):
p[6]+=1
if (map[i3][j3] == k and i3 - i == j - j3 and i3 >= i and j3 <= j):
p[7]+=1
if (map[i3][j3] == k and j - j3 == i - i3 and i3 <= i + 1 and i3 >= i - 3 and j3 <= j + 1 and j3 >= j - 3):
p[8]+=1
if (map[i3][j3] == k and j == j3 and i3 <= i + 1 and i3 >= i - 3 and j3 <= j + 1 and j3 >= j - 3):
p[9]+=1
if (map[i3][j3] == k and i == i3 and i3 <= i + 1 and i3 >= i - 3 and j3 <= j + 1 and j3 >= j - 3):
p[10]+=1
if (map[i3][j3] == k and j - j3 == i - i3 and i3 >= i - 1 and i3 <= i + 3 and j3 >= j - 1 and j3 <= j + 3):
p[11]+=1
if (map[i3][j3] == k and j == j3 and i3 >= i - 1 and i3 <= i + 3 and j3 >= j - 1 and j3 <= j + 3):
p[12]+=1
if (map[i3][j3] == k and i == i3 and i3 >= i - 1 and i3 <= i + 3 and j3 >= j - 1 and j3 <= j + 3):
p[13]+=1
if (map[i3][j3] == k and i - i3 == j3 - j and i3 <= i + 1 and i3 >= i - 3 and j3 >= j - 1 and j3 <= j + 3):
p[14]+=1
if (map[i3][j3] == k and i3 - i == j - j3 and i3 >= i - 1 and i3 <= i + 3 and j3 <= j + 1 and j3 >= j - 3):
p[15]+=1
if (map[i3][j3] == k and j - j3 == i - i3 and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2):
p[16]+=1
if (map[i3][j3] == k and j == j3 and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2):
p[17]+=1
if (map[i3][j3] == k and i == i3 and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2):
p[18]+=1
if (map[i3][j3] == k and i - i3 == j3 - j and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2):
p[19]+=1
for b in range(20):
if p[b]==5:
return True
return False
#繪制提示器(類容,屏幕,字大小)
def text(s,screen,x):
#先把上一次的類容用一個矩形覆蓋
pygame.draw.rect(screen,background,[850,100,1200,100])
#定義字體跟大小
s_font=pygame.font.Font('font.ttf',x)
#定義類容,是否抗鋸齒,顏色
s_text=s_font.render(s,True,button)
#將字放在窗口指定位置
screen.blit(s_text,(880,100))
pygame.display.flip()
#用於控制順序
t=True
#用於結束游戲後阻止落子
running=True
#主函數
def main():
#將 t,map,running設置為可改的
global t,map,running,maps,r,h
#將map置零
clear()
#定義儲存所有棋盤狀態的列表(用於悔棋)
map2=.deep(map)
maps=[map2]
#定義窗口
screen = pygame.display.set_mode([1200,806])
#定義窗口名字
pygame.display.set_caption("五子棋")
#在窗口畫出棋盤,提示器以及按鈕
Draw_a_chessboard(screen)
pygame.display.flip()
clock=pygame.time.Clock()
while True:
#只有running為真才能落子,主要用於游戲結束後防止再次落子
if running:
if t:
color=1
text('黑棋落子',screen,54)
else:
color=2
text('白棋落子',screen,54)
for event in pygame.event.get():
#點擊x則關閉窗口
if event.type ==pygame.QUIT:
pygame.quit()
sys.exit()
#點擊窗口裡面類容則完成相應指令
elif event.type == MOUSEBUTTONDOWN:
if event.button == 1:
x,y=event.pos[0],event.pos[1]
for i in range(19):
for j in range(19):
#點擊棋盤相應位置
if i*40+3+20<x<i*40+3+60 and j*40+3+20<y<j*40+3+60 and not map[i][j] and running:
#在棋盤相應位置落相應顏色棋子
Draw_a_chessman(i+1,j+1,screen,color)
#播放音效
play_chess_sound.play(0)
#在map裡面記錄落子位置
map[i][j]=color
#將map存入maps
map3=.deep(map)
maps.append(map3)
#判斷落子後是否有五子一線
if win(i,j):
if t:
text('黑棋勝利,請重新游戲',screen,30)
else:
text('白棋勝利,請重新游戲',screen,30)
#播放音效
victor_sound.play(0)
#阻止再往棋盤落子
running=False
pygame.display.flip()
t=not t
#如果點擊『重新開始』
if 900<x<1100 and 500<y<600:
#取消阻止
running=True
#播放音效
button_sound.play(0)
#重新開始
main()
#點擊『退出遊戲』,退出遊戲
elif 900<x<1100 and 650<y<750:
#播放音效
button_sound.play(0)
pygame.quit()
sys.exit()
#點擊『悔棋』
elif 900<x<1020 and 350<y<450 and len(maps)!=1:
#播放音效
button_sound.play(0)
#刪除maps里最後一個元素
del maps[len(maps)-1]
#再將最後一個元素給map
map=.deep(maps[len(maps)-1])
#切換順序
t=not t
#將map顯示出來
Draw_a_chessboard_with_chessman(map,screen)
#悔棋完成,阻止再次悔棋
x,y=0,0
clock.tick(60)
if __name__ == "__main__":
try:
main()
except SystemExit:
pass
except:
traceback.print_exc()
pygame.quit()
input()