五子棋的代码(五子棋代码详解)
admin 发布:2022-12-19 22:47 90
本篇文章给大家谈谈五子棋的代码,以及五子棋代码详解对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
五子棋源代码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
求一个简单的JAVA五子棋代码!! 网上复制的别来了!
以下是现写的 实现了两人对战 自己复制后运行把 没什么难度 类名 Games
import java.util.Scanner;
public class Games {
private String board[][];
private static int SIZE = 17;
private static String roles = "A玩家";
//初始化数组
public void initBoard() {
board = new String[SIZE][SIZE];
for (int i = 0; i SIZE; i++) {
for (int j = 0; j SIZE; j++) {
// if(i==0){
// String str = "";
// str += j+" ";
// board[i][j]= str;
// }else if(i!=0j==0){
// String str = "";
// str += i+" ";
// board[i][j]= str;
// }else{
board[i][j] = "╋";
// }
}
}
}
//输出棋盘
public void printBoard() {
for (int i = 0; i SIZE; i++) {
for (int j = 0; j SIZE; j++) {
System.out.print(board[i][j]);
}
System.out.println();
}
}
//判断所下棋子位置是否合理
public boolean isOk(int x, int y) {
boolean isRight = true;
if (x = 16 || x 1 || y = 16 | y 1) {
//System.out.println("输入错误,请从新输入");
isRight = false;
}
if (board[x][y].equals("●") || board[x][y].equals("○")) {
isRight = false;
}
return isRight;
}
//判断谁赢了
public void whoWin(Games wz) {
// 从数组挨个查找找到某个类型的棋子就从该棋子位置向右,向下,斜向右下 各查找5连续的位置看是否为5个相同的
int xlabel;// 记录第一次找到某个棋子的x坐标
int ylabel;// 记录第一次找到某个棋子的y坐标
// ●○╋
// 判断人是否赢了
for (int i = 0; i SIZE; i++) {
for (int j = 0; j SIZE; j++) {
if (board[i][j].equals("○")) {
xlabel = i;
ylabel = j;
// 横向找 x坐标不变 y坐标以此加1连成字符串
String heng = "";
if (i + 5 SIZE j + 5 SIZE) {
for (int k = j; k j + 5; k++) {
heng += board[i][k];
}
if (heng.equals("○○○○○")) {
System.out.println(roles+"赢了!您输了!");
System.exit(0);
}
// 向下判断y不变 x逐增5 连成字符串
String xia = "";
for (int l = j; l i + 5; l++) {
xia += board[l][j];
// System.out.println(xia);
}
if (xia.equals("○○○○○")) {
System.out.println(roles+"赢了!您输了!");
System.exit(0);
}
// 斜向右下判断
String youxia = "";
for (int a = 1; a = 5; a++) {
youxia += board[xlabel++][ylabel++];
}
if (youxia.equals("○○○○○")) {
System.out.println(roles+"赢了!您输了!");
System.exit(0);
}
}
}
}
}
// 判断电脑是否赢了
for (int i = 0; i SIZE; i++) {
for (int j = 0; j SIZE; j++) {
if (board[i][j].equals("●")) {
xlabel = i;
ylabel = j;
// 横向找 x坐标不变 y坐标以此加1连成字符串
String heng = "";
if (j + 5 SIZE i + 5 SIZE) {
for (int k = j; k j + 5; k++) {
heng += board[i][k];
}
if (heng.equals("●●●●●")) {
System.out.println(roles+"赢输了!您输了!");
System.exit(0);
}
// 向下判断y不变 x逐增5 连成字符串
String xia = "";
for (int l = i; l i + 5; l++) {
xia += board[l][ylabel];
// System.out.println(xia);
}
if (xia.equals("●●●●●")) {
System.out.println(roles+"赢了!您输了!");
System.exit(0);
}
// 斜向右下判断
String youxia = "";
for (int a = 1; a = 5; a++) {
youxia += board[xlabel++][ylabel++];
}
if (youxia.equals("●●●●●")) {
System.out.println(roles+"赢了!您输了!");
System.exit(0);
}
}
}
}
}
}
public static void main(String[] args) {
Games wz = new Games();
Scanner sc = new Scanner(System.in);
wz.initBoard();
wz.printBoard();
while (true) {
System.out.print("请"+roles+"输入X,Y坐标,必须在0-15范围内,xy以空格隔开,输入16 16结束程序");
int x = sc.nextInt();
int y = sc.nextInt();
if (x == SIZE y == SIZE) {
System.out.println("程序结束");
System.exit(0);
}
if (x SIZE || x 0 || y SIZE | y 0) {
System.out.println("输入错误,请从新输入");
continue;
}
//如果roles是A玩家 就让A玩家下棋,否则就让B玩家下棋。
if (wz.board[x][y].equals("╋")roles.equals("A玩家")) {
wz.board[x][y] = "○";
wz.printBoard();
//判断输赢
wz.whoWin(wz);
}else if(wz.board[x][y].equals("╋")roles.equals("B玩家")){
wz.board[x][y] = "●";
wz.printBoard();
//判断输赢
wz.whoWin(wz);
} else {
System.out.println("此处已经有棋子,从新输入");
continue;
}
if(roles.equals("A玩家")){
roles = "B玩家";
}else if(roles.equals("B玩家")){
roles = "A玩家";
}
}
}
}
求一个编写的五子棋小游戏python的代码,求借鉴啊!!急急急。。。
#五子棋import appuifw,e32,key_codes
from graphics import *def cn(x):return x.decode('utf-8')
def quit(): _quit=1
global running
running=1
def redraw(rect): canvas.blit(img)def default():
global con,color,font
con={"l":15,"x":15,"y":33,"r":13,"n":15}
color={"bg":0x7777bb,"fg":0x333333,"p1":0x000000,"p2":0xffffff,"w":0xff0000}
font=u"Sans MT 936_S60"def initial():
global img,canvas,con,color,cur_x,cur_y,turn,pos1,pos2,pos
appuifw.app.screen='full'
appuifw.app.body=canvas=appuifw.Canvas()
img=Image.new((240,320))
img.clear(color["bg"])
cur_x=7
cur_y=7
turn=1
pos1=[]
pos2=[]
pos=[]
for i in range(con["n"]*con["n"]):
pos.append(0)def paint_back():
global img,color,font
#img.text((90,25),cn('欢乐五子棋'),color["fg"],font)
for i in range(con["x"],con["x"]+con["l"]*con["n"]-1,con["l"]):
img.line((i,con["y"],i,con["y"]+con["l"]*(con["n"]-1)),color["fg"])
for i in range(con["y"],con["y"]+con["l"]*con["n"]-1,con["l"]):
img.line((con["x"],i,con["x"]+con["l"]*(con["n"]-1),i),color["fg"])
img.text((40,270),cn('玩家1'),color["p1"],font)
img.text((160,270),cn('玩家2'),color["p2"],font)
img.point((90,263),color["p1"],width=con["r"],fill=color["p1"])
img.point((144,263),color["p2"],width=con["r"],fill=color["p2"])
def paint_cur(x,y,sh):
global img,con,color,pos1,pos2,running
if running1:return
ax=con["x"]+con["l"]*x
ay=con["y"]+con["l"]*y
b=con["l"]/2
if sh0:
c=color["p"+str(sh)]
if rp((x,y))0:
c=color["w"]
if sh==0:
c=color["bg"]
img.line((ax-b,ay-2,ax-b,ay-b,ax-2,ay-b),c)
img.line((ax-b,ay+2,ax-b,ay+b,ax-2,ay+b),c)
img.line((ax+b,ay-2,ax+b,ay-b,ax+2,ay-b),c)
img.line((ax+b,ay+2,ax+b,ay+b,ax+2,ay+b),c)
redraw(())def paint_q(x,y,z):
global img,con,color
ax=con["x"]+con["l"]*x
ay=con["y"]+con["l"]*y
b=con["l"]/2
if z==0:
c=color["bg"]
else:
c=color["p"+str(z)]
img.point((ax,ay),c,width=con["r"],fill=c)
redraw(())
if z==0:
img.line((ax-b,ay,ax+b,ay),c)
img.line((ax,ay-b,ax,ay+b),c)
def k_up():
global cur_x,cur_y,con,turn
paint_cur(cur_x,cur_y,0)
cur_y=cur_y-1
if cur_y==-1:
cur_y=con["n"]-1
paint_cur(cur_x,cur_y,turn)def k_down():
global cur_x,cur_y,con,turn
paint_cur(cur_x,cur_y,0)
cur_y=cur_y+1
if cur_y==con["n"]:
cur_y=0
paint_cur(cur_x,cur_y,turn)def k_left():
global cur_x,cur_y,con,turn
paint_cur(cur_x,cur_y,0)
cur_x=cur_x-1
if cur_x==-1:
cur_x=con["n"]-1
paint_cur(cur_x,cur_y,turn)def k_right():
global cur_x,cur_y,con,turn
paint_cur(cur_x,cur_y,0)
cur_x=cur_x+1
if cur_x==con["n"]:
cur_x=0
paint_cur(cur_x,cur_y,turn)def rp(x):
global con,pos
if (x[0]0 or x[0]=con["n"] or x[1]0 or x[1]=con["n"]):return 0
#print x,pos[x[0]*con["n"]+x[1]]
return pos[x[0]*con["n"]+x[1]]def wp(x,y):
global con,pos
pos[x[0]*con["n"]+x[1]]=y
def win():
for i in pos1:
k=0
for j in range(0,6):
if rp((i[0]+j,i[1]))==1:
k=k+1
else:
break
if k=5:
return 1
k=0
for j in range(0,6):
if rp((i[0],i[1]+j))==1:
k=k+1
else:
break
if k=5:
return 1
k=0
for j in range(0,6):
if rp((i[0]+j,i[1]+j))==1:
k=k+1
else:
break
if k=5:
return 1
k=0
for j in range(0,6):
if rp((i[0]+j,i[1]-j))==1:
k=k+1
else:
break
if k=5:
return 1 for i in pos2:
k=0
for j in range(0,6):
if rp((i[0]+j,i[1]))==2:
k=k+1
else:
break
if k=5:
return 2
k=0
for j in range(0,6):
if rp((i[0],i[1]+j))==2:
k=k+1
else:
break
if k=5:
return 2
k=0
for j in range(0,6):
if rp((i[0]+j,i[1]+j))==2:
k=k+1
else:
break
if k=5:
return 2
k=0
for j in range(0,6):
if rp((i[0]+j,i[1]-j))==2:
k=k+1
else:
break
if k=5:
return 2
return 0
def k_enter():
global cur_x,cur_y,turn,pos1,pos2,con,color,font,running
if running1:return
if rp((cur_x,cur_y))==0:
if turn==1:
pos1.append((cur_x,cur_y))
img.rectangle((35,255,100,272),color["bg"])
img.rectangle((135,255,200,272),color["p2"])
if turn==2:
pos2.append((cur_x,cur_y))
img.rectangle((35,255,100,272),color["p1"])
img.rectangle((135,255,200,272),color["bg"])
paint_q(cur_x,cur_y,turn)
wp((cur_x,cur_y),turn)
if win()0:
#img.text((80,300),cn('玩家')+str(turn)+cn("获胜!"),color["fg"],font)
img.rectangle((35,255,100,272),color["bg"])
img.rectangle((135,255,200,272),color["bg"])
paint_cur(cur_x,cur_y,0)
running=2
turn=3-turn
paint_cur(cur_x,cur_y,turn)def bindkey():
canvas.bind(key_codes.EKeyUpArrow, k_up)
canvas.bind(key_codes.EKeyDownArrow,k_down)
canvas.bind(key_codes.EKeyLeftArrow, k_left)
canvas.bind(key_codes.EKeyRightArrow,k_right)
canvas.bind(key_codes.EKeySelect,k_enter)default()
initial()
paint_back()
paint_cur(cur_x,cur_y,1)
img.rectangle((35,255,100,272),color["p1"])
bindkey()redraw(())
appuifw.app.exit_key_handler = quit()
_quit=0
while (1-_quit):
e32.ao_sleep(0.2)
redraw(())
五子棋的代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于五子棋代码详解、五子棋的代码的信息别忘了在本站进行查找喔。
版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;
相关推荐
- 05-09网页代码,网页代码快捷键
- 05-06单页网站的代码(完整的网页代码)[20240506更新]
- 05-06个人主页图片代码(个人主页图片代码怎么弄)[20240506更新]
- 05-06提取微信名片代码(微信名片信息提取)[20240506更新]
- 05-06php后台权限管理代码(php管理员权限)[20240506更新]
- 05-06付费观看代码php(付费观看代码)[20240506更新]
- 05-06在线html执行代码(html怎么运行)[20240506更新]
- 05-06源代码管理资源管理器(资源管理器运行代码)[20240506更新]
- 05-06代码源软件库(程序代码库)[20240506更新]
- 05-06点击弹出密码代码(点击弹出密码代码错误)[20240506更新]
取消回复欢迎 你 发表评论:
- 标签列表
- 最近发表
- 友情链接