‘壹’ 求高手给出五子棋终结者的源代码~~
下面这个网址有各种VC代码,什么类别都有,不仅仅有五子棋的
http://www.vckbase.com/
‘贰’ 五子棋源代码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>
‘叁’ 急求vb双人对战五子棋源代码
Option Explicit
'五子棋程序 人机对战版本
'需要2个Label控件 2个CommandButton控件
Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
Private Declare Function CreateRoundRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long
'Dim PlayStep() As String '记录棋谱的数组
'Dim Label2Cap As String
Private Const BoxL As Single = 50, BoxT As Single = 50, BoxW As Single = 25, BoxN As Integer = 18
Dim Table() As Long '棋盘(0-BoxN,0-BoxN) 0-空 1-黑子 2-白子
Dim PsCore() As Long '定义当前玩家桌面空格的分数
Dim CsCore() As Long '定义当前电脑桌面空格的分数
Dim pWin() As Boolean '定义玩家的获胜组合
Dim cWin() As Boolean '定义电脑的获胜组合
Dim pFlag() As Boolean '定义玩家的获胜组合标志
Dim cFlag() As Boolean '定义电脑的获胜组合标志
Dim ThePlayFlag As Boolean '定义游戏有效标志
Private Sub Command1_Click()
If Not ThePlayFlag Then Call InitPlayEnvironment: Exit Sub
If MsgBox("本局还没有下完,是否重新开始?(Y/N)", vbYesNo) = vbNo Then Exit Sub
Call InitPlayEnvironment
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Form_Load()
Dim i As Long, lw As Long, lh As Long
'Label2Cap = "000 黑方 行 00 列 00"
Me.Width = 10815: Me.Height = 8040
' Me.Caption = "五子棋 - 人机对战": Me.Show
lw = Me.Width \ Screen.TwipsPerPixelX: lh = Me.Height \ Screen.TwipsPerPixelY
SetWindowRgn Me.hWnd, CreateRoundRectRgn(0, 0, lw, lh, 60, 60), True
With Label1
.Alignment = vbCenter: .FontSize = 12: .FontBold = True
.ForeColor = vbRed: .BackStyle = 0: .AutoSize = True: .Move 8910, 510
End With
Label2.AutoSize = True: Label2.WordWrap = True
Label2.BackStyle = 0: Label2.Move 8040, 1050, 2280
Command1.Move 8025, 7035, 1020, 435: Command1.Caption = "再来一局"
Command2.Move 9300, 7035, 1020, 435: Command2.Caption = "不玩了"
Call DrawChessBoard: Me.FillStyle = 0: Call InitPlayEnvironment
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
End
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim iRow As Long, iCol As Long, i As Long, k As Long, t As String
If Not ThePlayFlag Then Exit Sub
If Button = vbLeftButton Then '左键下棋
iRow = -1: iCol = -1
For i = 0 To BoxN '鼠标必须落在交叉点 半径10以内 若是则给出行列号
If (Y + 10) > (BoxT + i * BoxW) And (Y - 10) <= (BoxT + i * BoxW) Then iRow = i
If (X + 10) > (BoxL + i * BoxW) And (X - 10) <= (BoxL + i * BoxW) Then iCol = i
Next
If (iRow = -1) Or (iCol = -1) Then Beep: Exit Sub
If Table(iCol, iRow) > 0 Then Exit Sub
Table(iCol, iRow) = 2: Label1.Caption = "下一步 黑方"
Me.FillColor = vbWhite: Me.Circle (iCol * BoxW + BoxT, iRow * BoxW + BoxL), 8
For i = 0 To UBound(cWin, 3)
If cWin(iCol, iRow, i) = True Then cFlag(i) = False
Next
Call CheckWin: Call DianNao '检查当前玩家是否获胜 调用电脑算法
End If
End Sub
Public Sub InitPlayEnvironment()
'*****************************************************************************
' 模块名称: InitPlayEnvironment [初始化过程]
'
' 描述: 1. 设置背景音乐。 2. 设置游戏状态有效。
' 3. 初始化游戏状态标签。 4. 直接指定电脑的第一步走法。
' 5. 初始化基本得分桌面。 6. 电脑和玩家获胜标志初始化。
' 7. 初始化所有获胜组合。 8. 重新设定玩家的获胜标志。
'*****************************************************************************
Dim i As Long, j As Long, m As Long, n As Long
ThePlayFlag = True: Label1.Caption = "下一步 白方": Label2.Caption = ""
Me.FillColor = vbBlack: Me.FillStyle = 0: Me.AutoRedraw = True
Me.Cls: Me.Circle (9 * BoxW + BoxL, 9 * BoxW + BoxT), 8
ReDim Table(0 To BoxN, 0 To BoxN) As Long
ReDim pFlag(NumsWin(BoxN + 1) - 1) As Boolean
ReDim cFlag(UBound(pFlag)) As Boolean
ReDim PsCore(BoxN, BoxN) As Long, CsCore(BoxN, BoxN) As Long
ReDim pWin(BoxN, BoxN, UBound(pFlag)) As Boolean
ReDim cWin(BoxN, BoxN, UBound(pFlag)) As Boolean
For i = 0 To UBound(pFlag): pFlag(i) = True: cFlag(i) = True: Next
Table(9, 9) = 1 '假定电脑先手 并下了(9, 9)位 将其值设为1
'******** 初始化获胜组合 ****************************************
For i = 0 To BoxN: For j = 0 To BoxN - 4
For m = 0 To 4
pWin(j + m, i, n) = True: cWin(j + m, i, n) = True
Next
n = n + 1
Next: Next
For i = 0 To BoxN: For j = 0 To BoxN - 4
For m = 0 To 4
pWin(i, j + m, n) = True: cWin(i, j + m, n) = True
Next
n = n + 1
Next: Next
For i = 0 To BoxN - 4: For j = 0 To BoxN - 4
For m = 0 To 4
pWin(j + m, i + m, n) = True: cWin(j + m, i + m, n) = True
Next
n = n + 1
Next: Next
For i = 0 To BoxN - 4: For j = BoxN To 4 Step -1
For m = 0 To 4
pWin(j - m, i + m, n) = True: cWin(j - m, i + m, n) = True
Next
n = n + 1
Next: Next
'******** 初始化获胜组合结束 *************************************
For i = 0 To UBound(pWin, 3) '由于电脑已下了(9, 9)位 所以需要重新设定玩家的获胜标志
If pWin(9, 9, i) = True Then pFlag(i) = False
Next
End Sub
Public Function DrawChessBoard() As Long
'容器的(BoxL, BoxT)为左上角坐标画一个 BoxN*BoxN, 每格边长为 BoxW 象素的棋盘
Dim i As Long, j As Long, cx As Long, cy As Long
Me.ScaleMode = 3: Me.FillStyle = 1: Me.AutoRedraw = True: Me.Cls
For i = 0 To BoxN '画棋盘
Me.Line (BoxL + i * BoxW, BoxT)-(BoxL + i * BoxW, BoxT + BoxN * BoxW)
Me.Line (BoxL, BoxT + i * BoxW)-(BoxL + BoxN * BoxW, BoxT + i * BoxW)
Me.CurrentX = BoxL + i * BoxW - IIf(i > 9, 6, 2)
Me.CurrentY = BoxT - 20: Me.Print Format(i)
Me.CurrentX = BoxL - IIf(i > 9, 23, 20)
Me.CurrentY = BoxT + i * BoxW - 6: Me.Print Format(i)
Next
For i = 3 To 16 Step 6: For j = 3 To 16 Step 6 '画小标志
cx = BoxL + j * BoxW - 3: cy = BoxT + i * BoxW - 3
Me.Line (cx, cy)-(cx + 6, cy + 6), , B
Next: Next
Me.AutoRedraw = False: Set Me.Picture = Me.Image
End Function
Public Sub CheckWin()
'*****************************************************************************
' 模块名称: CheckWin [获胜检查算法]
'
' 描述: 1. 检查是否和棋。 2. 检查电脑是否获胜。 3. 检查玩家是否获胜。
'*****************************************************************************
Dim i As Long, j As Long, k As Long, m As Long, n As Long
Dim cA As Long, pA As Long, cN As Long
For i = 0 To UBound(cFlag): cN = IIf(cFlag(i) = False, cN + 1, cN): Next
If cN = UBound(cFlag) - 1 Then '设定和棋规则
Label1.Caption = "双方和棋!": ThePlayFlag = False: Exit Sub
End If
For i = 0 To UBound(cFlag) '检查电脑是否获胜
If cFlag(i) = True Then
cA = 0: For j = 0 To BoxN: For k = 0 To BoxN
If Table(j, k) = 1 And cWin(j, k, i) = True Then cA = cA + 1
Next: Next
If cA = 5 Then Label1.Caption = "电脑获胜!": ThePlayFlag = False: Exit Sub
End If
Next
For i = 0 To UBound(pFlag) '检查玩家是否获胜
If pFlag(i) = True Then
pA = 0: For j = 0 To BoxN: For k = 0 To BoxN
If Table(j, k) = 2 And pWin(j, k, i) = True Then pA = pA + 1
Next: Next
If pA = 5 Then Label1.Caption = "玩家获胜!": ThePlayFlag = False: Exit Sub
End If
Next
End Sub
Public Sub DianNao()
'*****************************************************************************
' 模块名称: DianNao [电脑算法]
' 描述: 1. 初始化赋值系统。 2. 赋值加强算法。 3. 计算电脑和玩家的最佳攻击位。
' 4. 比较电脑和玩家的最佳攻击位并决定电脑的最佳策略。 5. 执行检查获胜函数。
'*****************************************************************************
Dim i As Long, j As Long, k As Long, m As Long, n As Long
Dim Dc As Long, cAb As Long, pAb As Long
ReDim PsCore(BoxN, BoxN) As Long, CsCore(BoxN, BoxN) As Long '初始化赋值数组
'******** 电脑加强算法 ********
For i = 0 To UBound(cFlag)
If cFlag(i) = True Then
cAb = 0
For j = 0 To BoxN: For k = 0 To BoxN
If Table(j, k) = 1 And cWin(j, k, i) = True Then cAb = cAb + 1
Next: Next
Select Case cAb
Case 3
For m = 0 To BoxN: For n = 0 To BoxN
If Table(m, n) = 0 And cWin(m, n, i) = True Then CsCore(m, n) = CsCore(m, n) + 5
Next: Next
Case 4
For m = 0 To BoxN: For n = 0 To BoxN
If Table(m, n) = 0 And cWin(m, n, i) = True Then
Table(m, n) = 1: Label1.Caption = "下一步 白方"
Me.FillColor = vbBlack: Me.Circle (m * BoxW + BoxL, n * BoxW + BoxT), 8
For Dc = 0 To UBound(pWin, 3)
If pWin(m, n, Dc) = True Then pFlag(Dc) = False: Call CheckWin: Exit Sub
Next
End If
Next: Next
End Select
End If
Next
For i = 0 To UBound(pFlag)
If pFlag(i) = True Then
pAb = 0
For j = 0 To BoxN: For k = 0 To BoxN
If Table(j, k) = 2 And pWin(j, k, i) = True Then pAb = pAb + 1
Next: Next
Select Case pAb
Case 3
For m = 0 To BoxN: For n = 0 To BoxN
If Table(m, n) = 0 And pWin(m, n, i) = True Then PsCore(m, n) = PsCore(m, n) + 30
Next: Next
Case 4
For m = 0 To BoxN: For n = 0 To BoxN
If Table(m, n) = 0 And pWin(m, n, i) = True Then
Table(m, n) = 1: Label1.Caption = "下一步 白方"
Me.FillColor = vbBlack: Me.Circle (m * BoxW + BoxL, n * BoxW + BoxT), 8
For Dc = 0 To UBound(pWin, 3)
If pWin(m, n, Dc) = True Then pFlag(Dc) = False: Call CheckWin: Exit Sub
Next
End If
Next: Next
End Select
End If
Next
'******** 电脑加强算法结束 ********
'******** 赋值系统 ****************
For i = 0 To UBound(cFlag)
If cFlag(i) = True Then
For j = 0 To BoxN: For k = 0 To BoxN
If (Table(j, k) = 0) And cWin(j, k, i) Then
For m = 0 To BoxN: For n = 0 To BoxN
If (Table(m, n) = 1) And cWin(m, n, i) Then CsCore(j, k) = CsCore(j, k) + 1
Next: Next
End If
Next: Next
End If
Next
For i = 0 To UBound(pFlag)
If pFlag(i) = True Then
For j = 0 To BoxN: For k = 0 To BoxN
If (Table(j, k) = 0) And pWin(j, k, i) Then
For m = 0 To BoxN: For n = 0 To BoxN
If (Table(m, n) = 2) And pWin(m, n, i) Then PsCore(j, k) = PsCore(j, k) + 1
Next: Next
End If
Next: Next
End If
Next
'******** 赋值系统结束 ************
'******** 分值比较算法 ************
Dim a As Long, b As Long, c As Long, d As Long
Dim cS As Long, pS As Long
For i = 0 To BoxN: For j = 0 To BoxN
If CsCore(i, j) > cS Then cS = CsCore(i, j): a = i: b = j
Next: Next
For i = 0 To BoxN: For j = 0 To BoxN
If PsCore(i, j) > pS Then pS = PsCore(i, j): c = i: d = j
Next: Next
If cS > pS Then
Table(a, b) = 1: Label1.Caption = "下一步 白方"
Me.FillColor = vbBlack: Me.Circle (a * BoxW + BoxL, b * BoxW + BoxT), 8
For i = 0 To UBound(pWin, 3)
If pWin(a, b, i) = True Then pFlag(i) = False
Next
Else
Table(c, d) = 1: Label1.Caption = "下一步 白方"
Me.FillColor = vbBlack: Me.Circle (c * BoxW + BoxL, d * BoxW + BoxL), 8
For i = 0 To UBound(pWin, 3)
If pWin(c, d, i) = True Then pFlag(i) = False
Next
End If
'******** 分值比较算法结束 ********
Call CheckWin
End Sub
Public Function NumsWin(ByVal n As Long) As Long
'根据输入的棋盘布局 n*n 计算总共有多少种获胜组合
'假定棋盘为 10 * 10 相应的棋盘数组就是 Table(9, 9)
'水平方向 每一列获胜组合是6 共10列 6*10=60
'垂直方向 每一行获胜组合是6 共10行 8*10=60
'正对角线方向 6 + (5 + 4 + 3 + 2 + 1) * 2 = 36
'反对角线方向 6 + (5 + 4 + 3 + 2 + 1) * 2 = 36
'总的获胜组合数为 60 + 60 + 36 + 36 = 192
Dim i As Long, t As Long
For i = n - 5 To 1 Step -1: t = t + i: Next
NumsWin = 2 * (2 * t + n - 4) + 2 * n * (n - 4)
End Function
‘肆’ 找五子棋源代码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分
}
‘伍’ 求五子棋判别输赢的源代码。棋盘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()
‘柒’ 求极简单五子棋c\c++源码
你运气好我有收藏的、、
#include <stdio.h>
#include"bios.h"
#include <ctype.h>
#include <conio.h>
#include <dos.h>
#define CROSSRU 0xbf /*右上角点*/
#define CROSSLU 0xda /*左上角点*/
#define CROSSLD 0xc0 /*左下角点*/
#define CROSSRD 0xd9 /*右下角点*/
#define CROSSL 0xc3 /*左边*/
#define CROSSR 0xb4 /*右边*/
#define CROSSU 0xc2 /*上边*/
#define CROSSD 0xc1 /*下边*/
#define CROSS 0xc5 /*十字交叉点*/
/*定义棋盘左上角点在屏幕上的位置*/
#define MAPXOFT 5
#define MAPYOFT 2
/*定义1号玩家的操作键键码*/
#define PLAY1UP 0x1157/*上移--'W'*/
#define PLAY1DOWN 0x1f53/*下移--'S'*/
#define PLAY1LEFT 0x1e41/*左移--'A'*/
#define PLAY1RIGHT 0x2044/*右移--'D'*/
#define PLAY1DO 0x3920/*落子--空格键*/
/*定义2号玩家的操作键键码*/
#define PLAY2UP 0x4800/*上移--方向键up*/
#define PLAY2DOWN 0x5000/*下移--方向键down*/
#define PLAY2LEFT 0x4b00/*左移--方向键left*/
#define PLAY2RIGHT 0x4d00/*右移--方向键right*/
#define PLAY2DO 0x1c0d/*落子--回车键Enter*/
/*若想在游戏中途退出, 可按 Esc 键*/
#define ESCAPE 0x011b
/*定义棋盘上交叉点的状态, 即该点有无棋子 */
/*若有棋子, 还应能指出是哪个玩家的棋子 */
#define CHESSNULL 0 /*没有棋子*/
#define CHESS1 'O'/*一号玩家的棋子*/
#define CHESS2 'X'/*二号玩家的棋子*/
/*定义按键类别*/
#define KEYEXIT 0/*退出键*/
#define KEYFALLCHESS 1/*落子键*/
#define KEYMOVECURSOR 2/*光标移动键*/
#define KEYINVALID 3/*无效键*/
/*定义符号常量: 真, 假 --- 真为1, 假为0 */
#define TRUE 1
#define FALSE 0
/**********************************************************/
/* 定义数据结构 */
/*棋盘交叉点坐标的数据结构*/
struct point
{
int x,y;
};
/**********************************************************/
/*自定义函数原型说明 */
void Init(void);
int GetKey(void);
int CheckKey(int press);
int ChangeOrder(void);
int ChessGo(int Order,struct point Cursor);
void DoError(void);
void DoOK(void);
void DoWin(int Order);
void MoveCursor(int Order,int press);
void DrawCross(int x,int y);
void DrawMap(void);
int JudgeWin(int Order,struct point Cursor);
int JudgeWinLine(int Order,struct point Cursor,int direction);
void ShowOrderMsg(int Order);
void EndGame(void);
/**********************************************************/
/**********************************************************/
/* 定义全局变量 */
int gPlayOrder; /*指示当前行棋方 */
struct point gCursor; /*光标在棋盘上的位置 */
char gChessBoard[19][19];/*用于记录棋盘上各点的状态*/
/**********************************************************/
/**********************************************************/
/*主函数*/
void main()
{
int press;
int bOutWhile=FALSE;/*退出循环标志*/
Init();/*初始化图象,数据*/
while(1)
{
press=GetKey();/*获取用户的按键值*/
switch(CheckKey(press))/*判断按键类别*/
{
/*是退出键*/
case KEYEXIT:
clrscr();/*清屏*/
bOutWhile = TRUE;
break;
/*是落子键*/
case KEYFALLCHESS:
if(ChessGo(gPlayOrder,gCursor)==FALSE)/*走棋*/
DoError();/*落子错误*/
else
{
DoOK();/*落子正确*/
/*如果当前行棋方赢棋*/
if(JudgeWin(gPlayOrder,gCursor)==TRUE)
{
DoWin(gPlayOrder);
bOutWhile = TRUE;/*退出循环标志置为真*/
}
/*否则*/
else
/*交换行棋方*/
ChangeOrder();
ShowOrderMsg(gPlayOrder);
}
break;
/*是光标移动键*/
case KEYMOVECURSOR:
MoveCursor(gPlayOrder,press);
break;
/*是无效键*/
case KEYINVALID:
break;
}
if(bOutWhile==TRUE)
break;
}
/*游戏结束*/
EndGame();
}
/**********************************************************/
/*界面初始化,数据初始化*/
void Init(void)
{
int i,j;
char *Msg[]=
{
"Player1 key:",
" UP----w",
" DOWN--s",
" LEFT--a",
" RIGHT-d",
" DO----space",
"",
"Player2 key:",
" UP----up",
" DOWN--down",
" LEFT--left",
" RIGHT-right",
" DO----ENTER",
"",
"exit game:",
" ESC",
NULL,
};
/* 先手方为1号玩家 */
gPlayOrder = CHESS1;
/* 棋盘数据清零, 即棋盘上各点开始的时候都没有棋子 */
for(i=0;i<19;i++)
for(j=0;j<19;j++)
gChessBoard[i][j]=CHESSNULL;
/*光标初始位置*/
gCursor.x=gCursor.y=0;
/*画棋盘*/
textmode(C40);
DrawMap();
/*显示操作键说明*/
i=0;
textcolor(BROWN);
while(Msg[i]!=NULL)
{
gotoxy(25,3+i);
cputs(Msg[i]);
i++;
}
/*显示当前行棋方*/
ShowOrderMsg(gPlayOrder);
/*光标移至棋盘的左上角点处*/
gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT);
}
/*画棋盘*/
void DrawMap(void)
{
int i,j;
clrscr();
for(i=0;i<19;i++)
for(j=0;j<19;j++)
DrawCross(i,j);
}
/*画棋盘上的交叉点*/
void DrawCross(int x,int y)
{
gotoxy(x+MAPXOFT,y+MAPYOFT);
/*交叉点上是一号玩家的棋子*/
if(gChessBoard[x][y]==CHESS1)
{
textcolor(LIGHTBLUE);
putch(CHESS1);
return;
}
/*交叉点上是二号玩家的棋子*/
if(gChessBoard[x][y]==CHESS2)
{
textcolor(LIGHTRED);
putch(CHESS2);
return;
}
textcolor(GREEN);
/*左上角交叉点*/
if(x==0&&y==0)
{
putch(CROSSLU);
return;
}
/*左下角交叉点*/
if(x==0&&y==18)
{
putch(CROSSLD);
return;
}
/*右上角交叉点*/
if(x==18&&y==0)
{
putch(CROSSRU);
return;
}
/*右下角交叉点*/
if(x==18&&y==18)
{
putch(CROSSRD);
return;
}
/*左边界交叉点*/
if(x==0)
{
putch(CROSSL);
return;
}
/*右边界交叉点*/
if(x==18)
{
putch(CROSSR);
return;
}
/*上边界交叉点*/
if(y==0)
{
putch(CROSSU);
return;
}
/*下边界交叉点*/
if(y==18)
{
putch(CROSSD);
return;
}
/*棋盘中间的交叉点*/
putch(CROSS);
}
/*交换行棋方*/
int ChangeOrder(void)
{
if(gPlayOrder==CHESS1)
gPlayOrder=CHESS2;
else
gPlayOrder=CHESS1;
return(gPlayOrder);
}
/*获取按键值*/
int GetKey(void)
{
char lowbyte;
int press;
while (bioskey(1) == 0)
;/*如果用户没有按键,空循环*/
press=bioskey(0);
lowbyte=press&0xff;
press=press&0xff00 + toupper(lowbyte);
return(press);
}
/*落子错误处理*/
void DoError(void)
{
sound(1200);
delay(50);
nosound();
}
/*赢棋处理*/
void DoWin(int Order)
{
sound(1500);delay(100);
sound(0); delay(50);
sound(800); delay(100);
sound(0); delay(50);
sound(1500);delay(100);
sound(0); delay(50);
sound(800); delay(100);
sound(0); delay(50);
nosound();
textcolor(RED+BLINK);
gotoxy(25,20);
if(Order==CHESS1)
cputs("PLAYER1 WIN!");
else
cputs("PLAYER2 WIN!");
gotoxy(25,21);
cputs("\n");
getch();
}
/*走棋*/
int ChessGo(int Order,struct point Cursor)
{
/*判断交叉点上有无棋子*/
if(gChessBoard[Cursor.x][Cursor.y]==CHESSNULL)
{
/*若没有棋子, 则可以落子*/
gotoxy(Cursor.x+MAPXOFT,Cursor.y+MAPYOFT);
textcolor(LIGHTBLUE);
putch(Order);
gotoxy(Cursor.x+MAPXOFT,Cursor.y+MAPYOFT);
gChessBoard[Cursor.x][Cursor.y]=Order;
return TRUE;
}
else
return FALSE;
}
/*判断当前行棋方落子后是否赢棋*/
int JudgeWin(int Order,struct point Cursor)
{
int i;
for(i=0;i<4;i++)
/*判断在指定方向上是否有连续5个行棋方的棋子*/
if(JudgeWinLine(Order,Cursor,i))
return TRUE;
return FALSE;
}
/*判断在指定方向上是否有连续5个行棋方的棋子*/
int JudgeWinLine(int Order,struct point Cursor,int direction)
{
int i;
struct point pos,dpos;
const int testnum = 5;
int count;
switch(direction)
{
case 0:/*在水平方向*/
pos.x=Cursor.x-(testnum-1);
pos.y=Cursor.y;
dpos.x=1;
dpos.y=0;
break;
case 1:/*在垂直方向*/
pos.x=Cursor.x;
pos.y=Cursor.y-(testnum-1);
dpos.x=0;
dpos.y=1;
break;
case 2:/*在左下至右上的斜方向*/
pos.x=Cursor.x-(testnum-1);
pos.y=Cursor.y+(testnum-1);
dpos.x=1;
dpos.y=-1;
break;
case 3:/*在左上至右下的斜方向*/
pos.x=Cursor.x-(testnum-1);
pos.y=Cursor.y-(testnum-1);
dpos.x=1;
dpos.y=1;
break;
}
count=0;
for(i=0;i<testnum*2+1;i++)/*????????i<testnum*2-1*/
{
if(pos.x>=0&&pos.x<=18&&pos.y>=0&&pos.y<=18)
{
if(gChessBoard[pos.x][pos.y]==Order)
{
count++;
if(count>=testnum)
return TRUE;
}
else
count=0;
}
pos.x+=dpos.x;
pos.y+=dpos.y;
}
return FALSE;
}
/*移动光标*/
void MoveCursor(int Order,int press)
{
switch(press)
{
case PLAY1UP:
if(Order==CHESS1&&gCursor.y>0)
gCursor.y--;
break;
case PLAY1DOWN:
if(Order==CHESS1&&gCursor.y<18)
gCursor.y++;
break;
case PLAY1LEFT:
if(Order==CHESS1&&gCursor.x>0)
gCursor.x--;
break;
case PLAY1RIGHT:
if(Order==CHESS1&&gCursor.x<18)
gCursor.x++;
break;
case PLAY2UP:
if(Order==CHESS2&&gCursor.y>0)
gCursor.y--;
break;
case PLAY2DOWN:
if(Order==CHESS2&&gCursor.y<18)
gCursor.y++;
break;
case PLAY2LEFT:
if(Order==CHESS2&&gCursor.x>0)
gCursor.x--;
break;
case PLAY2RIGHT:
if(Order==CHESS2&&gCursor.x<18)
gCursor.x++;
break;
}
gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT);
}
/*游戏结束处理*/
void EndGame(void)
{
textmode(C80);
}
/*显示当前行棋方*/
void ShowOrderMsg(int Order)
{
gotoxy(6,MAPYOFT+20);
textcolor(LIGHTRED);
if(Order==CHESS1)
cputs("Player1 go!");
else
cputs("Player2 go!");
gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT);
}
/*落子正确处理*/
void DoOK(void)
{
sound(500);
delay(70);
sound(600);
delay(50);
sound(1000);
delay(100);
nosound();
}
/*检查用户的按键类别*/
int CheckKey(int press)
{
if(press==ESCAPE)
return KEYEXIT;/*是退出键*/
else
if
( ( press==PLAY1DO && gPlayOrder==CHESS1) ||
( press==PLAY2DO && gPlayOrder==CHESS2)
)
return KEYFALLCHESS;/*是落子键*/
else
if
( press==PLAY1UP || press==PLAY1DOWN ||
press==PLAY1LEFT || press==PLAY1RIGHT ||
press==PLAY2UP || press==PLAY2DOWN ||
press==PLAY2LEFT || press==PLAY2RIGHT
)
return KEYMOVECURSOR;/*是光标移动键*/
else
return KEYINVALID;/*按键无效*/
}
‘捌’ 五子棋源码
/*
五子棋
*/
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<bios.h>
#include<conio.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
#define SPACE 0x3920
#define BILI 20
#define JZ 4
#define JS 3
#define N 19
int box[N][N];
int step_x,step_y ;
int key ;
int flag=1 ;
void draw_box();
void draw_cicle(int x,int y,int color);
void change();
void judgewho(int x,int y);
void judgekey();
int judgeresult(int x,int y);
void attentoin();
void attention()
{
char ch ;
window(1,1,80,25);
textbackground(LIGHTBLUE);
textcolor(YELLOW);
clrscr();
gotoxy(15,2);
printf("游戏操作规则:");
gotoxy(15,4);
printf("Play Rules:");
gotoxy(15,6);
printf("1、按左右上下方向键移动棋子");
gotoxy(15,8);
printf("1. Press Left,Right,Up,Down Key to move Piece");
gotoxy(15,10);
printf("2、按空格确定落棋子");
gotoxy(15,12);
printf("2. Press Space to place the Piece");
gotoxy(15,14);
printf("3、禁止在棋盘外按空格");
gotoxy(15,16);
printf("3. DO NOT press Space outside of the chessboard");
gotoxy(15,18);
printf("你是否接受上述的游戏规则(Y/N)");
gotoxy(15,20);
printf("Do you accept the above Playing Rules? [Y/N]:");
while(1)
{
gotoxy(60,20);
ch=getche();
if(ch=='Y'||ch=='y')
break ;
else if(ch=='N'||ch=='n')
{
window(1,1,80,25);
textbackground(BLACK);
textcolor(LIGHTGRAY);
clrscr();
exit(0);
}
gotoxy(51,12);
printf(" ");
}
}
void draw_box()
{
int x1,x2,y1,y2 ;
setbkcolor(LIGHTBLUE);
setcolor(YELLOW);
gotoxy(7,2);
printf("Left, Right, Up, Down KEY to move, Space to put, ESC-quit.");
for(x1=1,y1=1,y2=18;x1<=18;x1++)
line((x1+JZ)*BILI,(y1+JS)*BILI,(x1+JZ)*BILI,(y2+JS)*BILI);
for(x1=1,y1=1,x2=18;y1<=18;y1++)
line((x1+JZ)*BILI,(y1+JS)*BILI,(x2+JZ)*BILI,(y1+JS)*BILI);
for(x1=1;x1<=18;x1++)
for(y1=1;y1<=18;y1++)
box[x1][y1]=0 ;
}
void draw_circle(int x,int y,int color)
{
setcolor(color);
setlinestyle(SOLID_LINE,0,1);
x=(x+JZ)*BILI ;
y=(y+JS)*BILI ;
circle(x,y,8);
}
void judgekey()
{
int i ;
int j ;
switch(key)
{
case LEFT :
if(step_x-1<0)
break ;
else
{
for(i=step_x-1,j=step_y;i>=1;i--)
if(box[i][j]==0)
{
draw_circle(step_x,step_y,LIGHTBLUE);
break ;
}
if(i<1)break ;
step_x=i ;
judgewho(step_x,step_y);
break ;
}
case RIGHT :
if(step_x+1>18)
break ;
else
{
for(i=step_x+1,j=step_y;i<=18;i++)
if(box[i][j]==0)
{
draw_circle(step_x,step_y,LIGHTBLUE);
break ;
}
if(i>18)break ;
step_x=i ;
judgewho(step_x,step_y);
break ;
}
case DOWN :
if((step_y+1)>18)
break ;
else
{
for(i=step_x,j=step_y+1;j<=18;j++)
if(box[i][j]==0)
{
draw_circle(step_x,step_y,LIGHTBLUE);
break ;
}
if(j>18)break ;
step_y=j ;
judgewho(step_x,step_y);
break ;
}
case UP :
if((step_y-1)<0)
break ;
else
{
for(i=step_x,j=step_y-1;j>=1;j--)
if(box[i][j]==0)
{
draw_circle(step_x,step_y,LIGHTBLUE);
break ;
}
if(j<1)break ;
step_y=j ;
judgewho(step_x,step_y);
break ;
}
case ESC :
break ;
case SPACE :
if(step_x>=1&&step_x<=18&&step_y>=1&&step_y<=18)
{
if(box[step_x][step_y]==0)
{
box[step_x][step_y]=flag ;
if(judgeresult(step_x,step_y)==1)
{
sound(1000);
delay(1000);
nosound();
gotoxy(30,4);
if(flag==1)
{
setbkcolor(BLUE);
cleardevice();
setviewport(100,100,540,380,1);
/*定义一个图形窗口*/
setfillstyle(1,2);
/*绿色以实填充*/
setcolor(YELLOW);
rectangle(0,0,439,279);
floodfill(50,50,14);
setcolor(12);
settextstyle(1,0,5);
/*三重笔划字体, 水平放?5倍*/
outtextxy(20,20,"The White Win !");
setcolor(15);
settextstyle(3,0,5);
/*无衬笔划字体, 水平放大5倍*/
outtextxy(120,120,"The White Win !");
setcolor(14);
settextstyle(2,0,8);
getch();
closegraph();
exit(0);
}
if(flag==2)
{
setbkcolor(BLUE);
cleardevice();
setviewport(100,100,540,380,1);
/*定义一个图形窗口*/
setfillstyle(1,2);
/*绿色以实填充*/
setcolor(YELLOW);
rectangle(0,0,439,279);
floodfill(50,50,14);
setcolor(12);
settextstyle(1,0,8);
/*三重笔划字体, 水平放大8倍*/
outtextxy(20,20,"The Red Win !");
setcolor(15);
settextstyle(3,0,5);
/*无衬笔划字体, 水平放大5倍*/
outtextxy(120,120,"The Red Win !");
setcolor(14);
settextstyle(2,0,8);
getch();
closegraph();
exit(0);
}
}
change();
break ;
}
}
else
break ;
}
}
void change()
{
if(flag==1)
flag=2 ;
else
flag=1 ;
}
void judgewho(int x,int y)
{
if(flag==1)
draw_circle(x,y,15);
if(flag==2)
draw_circle(x,y,4);
}
int judgeresult(int x,int y)
{
int j,k,n1,n2 ;
while(1)
{
n1=0 ;
n2=0 ;
/*水平向左数*/
for(j=x,k=y;j>=1;j--)
{
if(box[j][k]==flag)
n1++;
else
break ;
}
/*水平向右数*/
for(j=x,k=y;j<=18;j++)
{
if(box[j][k]==flag)
n2++;
else
break ;
}
if(n1+n2-1>=5)
{
return(1);
break ;
}
/*垂直向上数*/
n1=0 ;
n2=0 ;
for(j=x,k=y;k>=1;k--)
{
if(box[j][k]==flag)
n1++;
else
break ;
}
/*垂直向下数*/
for(j=x,k=y;k<=18;k++)
{
if(box[j][k]==flag)
n2++;
else
break ;
}
if(n1+n2-1>=5)
{
return(1);
break ;
}
/*向左上方数*/
n1=0 ;
n2=0 ;
for(j=x,k=y;j>=1,k>=1;j--,k--)
{
if(box[j][k]==flag)
n1++;
else
break ;
}
/*向右下方数*/
for(j=x,k=y;j<=18,k<=18;j++,k++)
{
if(box[j][k]==flag)
n2++;
else
break ;
}
if(n1+n2-1>=5)
{
return(1);
break ;
}
/*向右上方数*/
n1=0 ;
n2=0 ;
for(j=x,k=y;j<=18,k>=1;j++,k--)
{
if(box[j][k]==flag)
n1++;
else
break ;
}
/*向左下方数*/
for(j=x,k=y;j>=1,k<=18;j--,k++)
{
if(box[j][k]==flag)
n2++;
else
break ;
}
if(n1+n2-1>=5)
{
return(1);
break ;
}
return(0);
break ;
}
}
void main()
{
int gdriver=VGA,gmode=VGAHI;
clrscr();
attention();
initgraph(&gdriver,&gmode,"c:\\tc");
/* setwritemode(XOR_PUT);*/
flag=1 ;
draw_box();
do
{
step_x=0 ;
step_y=0 ;
/*draw_circle(step_x,step_y,8); */
judgewho(step_x-1,step_y-1);
do
{
while(bioskey(1)==0);
key=bioskey(0);
judgekey();
}
while(key!=SPACE&&key!=ESC);
}
while(key!=ESC);
closegraph();
}
‘玖’ 设计五子棋的具体步骤和源代码
已发 请查收