Адрес: ул. Б. Очаковская 32 Москва Россия
Наши официальные канал и чат в telegram, группа в ВКонтакте

Демо шахматы

Обычная демка, не полноценная игра

Библиотеки и примеры программ на языке Yabasic
Аватара пользователя
Anton
Site Admin
Сообщения: 137
Зарегистрирован: Чт фев 08, 2024 7:03 pm
Откуда: Москва

Демо шахматы

Сообщение Anton »

Демо шахматы
Это не полноценная игра, просто графическая демка.

Листинг программы:

Код: Выделить всё

'Chess
'Jim 22/5/2002

grid = 60
dim board(8,8)
setup_board()

open window 480,480

whosgo=1
d=1
repeat
drawboard()
makeamove(whosgo)
whosgo=1-whosgo

wait 1

until (0=1)

end

sub makeamove(g)
	if (g=1) then
		up = -1
		home = 6
		bend=0
		colf=8
	else
		up = 1
		home = 1
		bend=7
		colf=0
	fi

	for y = 0 to 7
	for x = 0 to 7
	piece = and(board(x,y),7)
	col = and(board(x,y),8)/8
	if col=g then
		if piece=1 then
			rem advance pawn
			if (x+1<=7) then
				if (and(board(x+1,y+up),7) <> 0) then
					if (y+up=bend) then
						board(x+1,y+up)=5+colf
					else
						board(x+1,y+up)=board(x,y)
					fi
					board(x,y)=0
					return
				fi
			fi
			if (x-1>=0) then
				if (and(board(x-1,y+up),7) <> 0) then
					if (y+up=bend) then
						board(x-1,y+up)=5+colf
					else
						board(x-1,y+up)=board(x,y)
					fi
					board(x,y)=0
					return
				fi
			fi
			if (board(x,y+2*up)=0 and y=home) then
				board(x,y+2*up)=board(x,y)
				board(x,y)=0
				return
			fi
			if (board(x,y+up)=0) then
				if (y+up=bend) then
					board(x,y+up)=5+colf
				else
					board(x,y+up)=board(x,y)
				fi
				board(x,y)=0
				return
			fi
		elsif piece=2 then
		elsif piece=3 then
		elsif piece=4 then
		elsif piece=5 then
		elsif piece=6 then
		fi
	fi
	next x
	next y
end sub

sub drawpiece(c)
	color c*255,c*255,c*255
	repeat
		read p$
		if p$="E" then
			return
		elsif p$="C" then
			read xs,ys,r
			fill circle xx+xs*grid,yy+ys*grid,r*grid
		elsif p$="R" then
			read xo,yo,xs,ys
			fill rectangle xx+xo*grid,yy+yo*grid to xx+xs*grid,yy+ys*grid
		elsif p$="T" then
			read x1,y1,x2,y2,x3,y3
			fill triangle xx+x1*grid,yy+y1*grid to xx+x2*grid,yy+y2*grid to xx+x3*grid,yy+y3*grid
		fi
	until (0=1)
end sub

sub pawn(x,y,c)
	restore pawnd
	drawpiece(c)
end sub

sub rook(x,y,c)
	restore rookd
	drawpiece(c)
end sub

sub knight(x,y,c)
	restore knightd
	drawpiece(c)
end sub

sub bishop(x,y,c)
	restore bishopd
	drawpiece(c)
end sub

sub queen(x,y,c)
	restore queend
	drawpiece(c)
end sub

sub king(x,y,c)
	restore kingd
	drawpiece(c)
end sub

sub drawboard()
	draw_checker()
	yy=0
	for y = 0 to 7
	xx=0
	for x = 0 to 7
	piece = and(board(x,y),7)
	col = and(board(x,y),8)/8
	if piece=1 then
		pawn(x,y,col)
	elsif piece=2 then
		rook(x,y,col)
	elsif piece=3 then
		knight(x,y,col)
	elsif piece=4 then
		bishop(x,y,col)
	elsif piece=5 then
		queen(x,y,col)
	elsif piece=6 then
		king(x,y,col)
	fi
	xx = xx+grid
	next x
	yy = yy+grid
	next y
end sub

sub draw_checker()
color 255,255,255
rectangle 0,0 to 8*grid+1,8*grid+2
toggle=128
for y=0 to 7
for x=0 to 7
color toggle,toggle,toggle
fill rectangle 1+x*grid,1+y*grid to 1+x*grid+grid,1+y*grid+grid
toggle = 192-toggle
next x
toggle = 192-toggle
next y
end sub

sub setup_board()
board(0,0)=2
board(1,0)=3
board(2,0)=4
board(3,0)=5
board(4,0)=6
board(5,0)=4
board(6,0)=3
board(7,0)=2
board(0,1)=1
board(1,1)=1
board(2,1)=1
board(3,1)=1
board(4,1)=1
board(5,1)=1
board(6,1)=1
board(7,1)=1

board(0,6)=1+8
board(1,6)=1+8
board(2,6)=1+8
board(3,6)=1+8
board(4,6)=1+8
board(5,6)=1+8
board(6,6)=1+8
board(7,6)=1+8
board(0,7)=2+8
board(1,7)=3+8
board(2,7)=4+8
board(3,7)=5+8
board(4,7)=6+8
board(5,7)=4+8
board(6,7)=3+8
board(7,7)=2+8
end sub

label pawnd
data "C",0.5,0.25,.2
data "T",0.5,0.25,0.2,.8,.8,.8
data "E"

label rookd
data "T",0.5,0.25,0.2,.8,.8,.8
data "R",.2,.3,.8,.35
data "R",.2,.1,.35,.3
data "R",.4,.1,.6,.3
data "R",.65,.1,.8,.3
data "E"

label knightd
data "T",0.5,0.25,0.2,.8,.8,.8
data "T",0.4,0.1,0.9,0.3,0.4,0.4
data "E"

label bishopd
data "T",0.5,0.25,0.2,.8,.8,.8
data "C",0.4,0.25,0.15
data "C",0.6,0.25,0.15
data "E"

label queend
data "T",0.5,0.25,0.2,.8,.8,.8
data "T",0.5,0.25,0.2,.1,.8,.1
data "E"

label kingd
data "T",0.5,0.25,0.2,.8,.8,.8
data "R",0.3,.15,0.7,.25
data "R",0.45,.05,0.55,0.4
data "E"
Приимер работы программы:
2025-05-16_17-19-50.gif
У вас нет необходимых прав для просмотра вложений в этом сообщении.