導航:首頁 > APP軟體 > 安卓五子棋游戲源代碼

安卓五子棋游戲源代碼

發布時間:2023-02-20 21:36:40

1. 急求五子棋源代碼

/*
五子棋
*/

#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();
}



2. 求一個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//圖標文件

若滿意請及時採納,謝謝

3. java五子棋源代碼

我這有演算法 不過沒做swing界面 DOS下可以直接運行 要不你拿去改改
import java.io.BufferedReader;
import java.io.InputStreamReader;

/*
* 五子棋源碼
* 所用的符號標識 ○ ● ┼
* 在dos界面下運行效果最佳
* 黑白雙方交叉輸入落子點坐標 以逗號隔開如 1,1
* 輸入空 或者一方勝出 程序停止
*/
public class Chess {
// 定義棋盤大小
private static int SIZE = 15;
private String[][] board;

public static void main(String[] args) throws Exception {
Chess chess = new Chess();
// 初始化棋盤
chess.initBoard();
// 畫出棋盤
chess.paintBoard();
// 根據who的奇偶性 判斷該誰落子
int who = 0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
while ((str = br.readLine()) != null) {
// 提取輸入的 以","分開的數 分別對應x y坐標
String[] posStr = str.split(",");
int x = Integer.parseInt(posStr[0]);
int y = Integer.parseInt(posStr[1]);
// 判斷落子點是否合法
if (!"┼".equals(chess.board[x][y])) {
System.out.println("這里不允許落子,請重下..");
continue;
}
if (who % 2 == 0) {
chess.board[x][y] = "○";
chess.paintBoard();
// 判斷是否勝出
if (chess.isWin("○")) {
System.out.println("○獲勝");
return;
}
} else {
chess.board[x][y] = "●";
chess.paintBoard();
// 判斷是否勝出
if (chess.isWin("●")) {
System.out.println("●獲勝");
return;
}
}

who++;
}
}

// 以 "┼" 初始化棋盤
public void initBoard() {
board = new String[SIZE][SIZE];
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
board[i][j] = "┼";
}
}
}

// 描繪出當前棋盤
public void paintBoard() {
// 以下代碼 這里為了使得棋盤坐標看的清楚 加入了坐標值
System.out.print(" ");
for (int i = 0; i < SIZE; i++) {
if (i < 10) {
System.out.print(i + " ");
} else {
System.out.print((i - 10) + " ");
}
}
System.out.println();
// 以上代碼 這里為了使得棋盤坐標看的清楚 加入了坐標值
for (int i = 0; i < SIZE; i++) {
if (i < 10) {
System.out.print(" " + i);
} else {
System.out.print(i);
}
for (int j = 0; j < SIZE; j++) {
System.out.print(board[i][j]);
}
System.out.println();

}
}

// 判斷是否獲勝
public boolean isWin(String sign) {
int count = 0;
// 橫向掃描各行
// 有一個sign的子 計數器+1
// 碰到不是sign的子 計數器置零
// 計數器到達5時 返回true 勝出
for (int i = 0; i < SIZE; i++) {
count = 0;
for (int j = 0; j < SIZE; j++) {
if (board[i][j].equals(sign)) {
count++;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
}
// 縱向掃描各列
// 方法同上
for (int i = 0; i < SIZE; i++) {
count = 0;
for (int j = 0; j < SIZE; j++) {
if (board[j][i].equals(sign)) {
count++;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
}
// 掃描斜右下
// 在橫向掃描基礎上 外層套一個循環 以k為標識
// 坐標x-y的范圍在-SIZE+1到SIZE-1之間
// 當x-y的值相等時 在同一右下斜線上
for (int k = -SIZE + 1; k <= SIZE - 1; k++) {
count = 0;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (i - j == k) {
if (board[i][j].equals(sign)) {
count++;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
}
}
}
// 掃描斜左邊上
// 方法同上 坐標x+y的值相等時 在同一左上斜線上
for (int k = -SIZE + 1; k <= SIZE - 1; k++) {
count = 0;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (i + j == k) {
if (board[i][j].equals(sign)) {
count++;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
}
}
}

return false;
}

}

4. 找五子棋源代碼c++

網上下載一個,再改改

5. 急求:五子棋的源代碼(數據結構),每一步都要有解釋的!!!

#include<iostream.h>#include<stdlib.h>#define Num 15//********************************************************//類class T//定義類用來封裝所有相關函數和變數{ char board[Num][Num];//用數組board[Num][Num]來定義棋盤public: void PrintMenu(); //列印菜單 說明游戲規則和方法 void PrintBoard(); //列印棋盤 void GameStart(char*,int &,int &,char); //下棋 int whichwin(int,int,char); //判斷那個選手贏 void Choice(char &); //是否再玩 void Setboard(); //重置棋盤};//****************************************************************//main主函數void main ()//主函數{ T s;//說明類的一個對象s s.PrintMenu();//通過s調用PrintMenu函數提示如何游戲 char player1[20],player2[20];//玩家姓名 int FirstWin=0,SecondWin=0,Draws=0,x,y,N;//說明變數,賦初值為0以待計算輸贏結果 char choice='Y'; cin.ignore(20,'\n');//輸入輸出流,前面如果有輸入把輸入行所有字元取空,以便後面的輸入從新的一行開始 cout<<"請輸入第一個玩家姓名:"; cin.getline(player1,20);//連續讀取數據 cout<<"請輸入第二個玩家姓名:"; cin.getline(player2,20); while(choice=='Y'||choice=='y')//條件成立,執行 { s.Setboard();//調用Setboard函數 N=0; while(N<=(Num*Num)) { s.PrintBoard();//列印棋盤 s.GameStart(player1,x,y,'O'); N++;//記錄已下棋子數 if(s.whichwin(x-1,y-1,'O'))//返回值不為0則條件成立 { s.PrintBoard(); cout<<player1<<"贏了。"<<endl; FirstWin++;//記錄贏局數 break;//終止本次循環 } s.PrintBoard();//同上 s.GameStart(player2,x,y,'X'); N++; if(s.whichwin(x-1,y-1,'X')) { s.PrintBoard(); cout<<player2<<"贏了。"<<endl; SecondWin++; break; } if(N==(Num*Num)) { cout<<"和棋!"; Draws++;//記錄平局數 break; } } s.Choice(choice);//給玩家提供一次選擇是否再玩的機會 } //輸出遊戲輸贏次數 cout<<player1<<"贏了"<<FirstWin<<"次"<<endl; cout<<player2<<"贏了"<<SecondWin<<"次"<<endl; cout<<"和"<<Draws<<"次"<<endl; cout<<"謝謝使用。"<<endl; cout<<"任意鍵繼續。"<<endl; cin.get();//很必要的,目的是空度換行字元}//*******************************************************************//定義公有成員函數void T::PrintMenu(){ cout<<"歡迎進入五子棋游戲!\n"; cout<<"******************************************"<<endl; cout<<"\t游戲說明:"<<endl<<endl; cout<<"1.第一個玩家用O第二個玩家用X;"<<endl; cout<<"2.請根據提示輸入所要走的行和列;"<<endl; cout<<"3.按<Enter>下棋。"<<endl; cout<<"

6. 在線等!求一個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()

7. 求一個C語言小程序(五子棋)源代碼

*******************************************************************/
/* ALEX_LEE 五子棋 C語言小程序 */
/* o(∩_∩)o...可以用來復習一下C語言的小程序 */
/* My Blog:hi..com/alexlee321 */
/******************************************************************/

/**********************************************************/
#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();
}
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(LIGHTBLUE);
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(" \\<^+^>/");
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++)
{
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;/*按鍵無效*/
}

8. 五子棋源代碼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>

9. 跪求C++五子棋程序源代碼

#include "iostream"
#include <iomanip>
using namespace std;
const int M=20;
const int N=20;
int main()
{
char wei[M][N];
int k,i,j,x,y,flag=0;
cout<<"歡迎使用簡易雙人對戰五子棋游戲"<<endl;
cout<<"五子棋棋譜如下:"<<endl;
for(k=0;k<=N;k++)
cout<<setw(3)<<setfill(' ')<<k;
cout<<endl;
for(i=1;i<=M;i++)
{
cout<<setw(3)<<setfill(' ')<<i;
for(j=1;j<=N;j++)
{
wei[i][j]='-';
cout<<setw(3)<<setfill(' ')<<wei[i][j];
}
cout<<endl;
}
while(flag==0)
{
//紅方落子
cout<<"請紅方輸入落子位置:"<<endl;
loop1:
cout<<"請輸入落子的行數:";
cin>>x;
cout<<"請輸入落子的列數:";
cin>>y;
if(wei[x][y]=='-')
{
wei[x][y]='*';
for(k=0;k<=N;k++)
cout<<setw(3)<<setfill(' ')<<k;
cout<<endl;
for(i=1;i<=M;i++)
{
cout<<setw(3)<<setfill(' ')<<i;
for(j=1;j<=N;j++)
cout<<setw(3)<<setfill(' ')<<wei[i][j];
cout<<endl;
}
}
else
{
cout<<"你不能在這落子,請重新選擇落子位置:"<<endl;
goto loop1;
}
//判斷勝利
for(i=1;i<=M-4;i++)
{
for(j=1;j<=N-4;j++)
{
if(wei[i][j]=='*' && wei[i][j+1]=='*' && wei[i][j+2]=='*' && wei[i][j+3]=='*' && wei[i][j+4]=='*')
{
cout<<"恭喜紅方獲得簡易雙人對戰五子棋的勝利!耶~~~"<<endl;
flag=1;
break;
}
if(wei[i][j]=='*' && wei[i+1][j]=='*' && wei[i+2][j]=='*' && wei[i+3][j]=='*' && wei[i+4][j]=='*')
{
cout<<"恭喜紅方獲得簡易雙人對戰五子棋的勝利!耶~~~"<<endl;
flag=1;
break;
}
if(wei[i][j]=='*' && wei[i+1][j+1]=='*' && wei[i+2][j+2]=='*' && wei[i+3][j+3]=='*' && wei[i+4][j+4]=='*')
{
cout<<"恭喜紅方獲得簡易雙人對戰五子棋的勝利!耶~~~"<<endl;
flag=1;
break;
}
if(flag==1)
break;
}
}

//藍方落子
cout<<"請藍方輸入落子位置:"<<endl;
loop2:
cout<<"請輸入落子的行數:";
cin>>x;
cout<<"請輸入落子的列數:";
cin>>y;
if(wei[x][y]=='-')
{
wei[x][y]='#';
for(k=0;k<=N;k++)
cout<<setw(3)<<setfill(' ')<<k;
cout<<endl;
for(i=1;i<=M;i++)
{
cout<<setw(3)<<setfill(' ')<<i;
for(j=1;j<=N;j++)
cout<<setw(3)<<setfill(' ')<<wei[i][j];
cout<<endl;
}
}
else
{
cout<<"你不能在這落子,請重新選擇落子位置:";
goto loop2;
}

//判斷勝利
for(i=1;i<=M-4;i++)
{
for(j=1;j<=N-4;j++)
{
if(wei[i][j]=='#' && wei[i][j+1]=='#' && wei[i][j+2]=='#' && wei[i][j+3]=='#' && wei[i][j+4]=='#')
{
cout<<"恭喜藍方獲得簡易雙人對戰五子棋的勝利!耶~~~"<<endl;
flag=1;
break;
}
if(wei[i][j]=='#' && wei[i+1][j]=='#' && wei[i+2][j]=='#' && wei[i+3][j]=='#' && wei[i+4][j]=='#')
{
cout<<"恭喜藍方獲得簡易雙人對戰五子棋的勝利!耶~~~"<<endl;
flag=1;
break;
}
if(wei[i][j]=='#' && wei[i+1][j+1]=='#' && wei[i+2][j+2]=='#' && wei[i+3][j+3]=='#' && wei[i+4][j+4]=='#')
{
cout<<"恭喜藍方獲得簡易雙人對戰五子棋的勝利!耶~~~"<<endl;
flag=1;
break;
}
if(flag==1)
break;
}
}
}
return 0;
}

二人對弈五子棋游戲,棋子分為黑白兩種;當同一種顏色的棋子實現五子連珠時即為獲勝;通過按下棋盤上的狀態鍵,用以標示該黑子落子還是白子落子;

運行成功;你可以在這基礎上修改一下

10. 求五子棋C源代碼

這個是稍微好一點的了,以前沒事試過

/*
五子棋
*/

#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();
}



閱讀全文

與安卓五子棋游戲源代碼相關的資料

熱點內容
僅限於文件系統的swf文件 瀏覽:994
海龍cad工具實用嗎 瀏覽:386
安卓開發java 瀏覽:209
大數據合約是什麼意思 瀏覽:955
2b數據線多少w 瀏覽:956
廈門雅迅網路怎麼樣 瀏覽:331
程序文件由哪些部分組成 瀏覽:703
java圖像解析度 瀏覽:174
智能家庭網路pdf 瀏覽:181
釘釘系app有哪些 瀏覽:426
word文檔紅頭文件頁面設置 瀏覽:630
京東白條能用微信還么 瀏覽:568
file標簽無後綴文件名 瀏覽:521
金數據如何設置投票後 瀏覽:245
文件格式轉換記事本百度經驗 瀏覽:664
win10所有exe文件失效 瀏覽:402
為什麼社交app加女朋友好友 瀏覽:598
2017網路爾雅課程答案 瀏覽:595
手機app客戶端ip是什麼 瀏覽:15
三線學兵連網站為什麼被關閉 瀏覽:121

友情鏈接