c小游戲源碼
『壹』 用C++編寫的小游戲源代碼
五子棋的代碼:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
using namespace std;
const int N=15; //15*15的棋盤
const char ChessBoardflag = ' '; //棋盤標志
const char flag1='o'; //玩家1或電腦的棋子標志
const char flag2='X'; //玩家2的棋子標志
typedef struct Coordinate //坐標類
{
int x; //代錶行
int y; //代表列
}Coordinate;
class GoBang //五子棋類
{
public:
GoBang() //初始化
{
InitChessBoard();
}
void Play() //下棋
{
Coordinate Pos1; // 玩家1或電腦
Coordinate Pos2; //玩家2
int n = 0;
while (1)
{
int mode = ChoiceMode();
while (1)
{
if (mode == 1) //電腦vs玩家
{
ComputerChess(Pos1,flag1); // 電腦下棋
if (GetVictory(Pos1, 0, flag1) == 1) //0表示電腦,真表示獲勝
break;
PlayChess(Pos2, 2, flag2); //玩家2下棋
if (GetVictory(Pos2, 2, flag2)) //2表示玩家2
break;
}
else //玩家1vs玩家2
{
PlayChess(Pos1, 1, flag1); // 玩家1下棋
if (GetVictory(Pos1, 1, flag1)) //1表示玩家1
break;
PlayChess(Pos2, 2, flag2); //玩家2下棋
if (GetVictory(Pos2, 2, flag2)) //2表示玩家2
break;
}
}
cout << "***再來一局***" << endl;
cout << "y or n :";
char c = 'y';
cin >> c;
if (c == 'n')
break;
}
}
protected:
int ChoiceMode() //選擇模式
{
int i = 0;
system("cls"); //系統調用,清屏
InitChessBoard(); //重新初始化棋盤
cout << "***0、退出 1、電腦vs玩家 2、玩家vs玩家***" << endl;
while (1)
{
cout << "請選擇:";
cin >> i;
if (i == 0) //選擇0退出
exit(1);
if (i == 1 || i == 2)
return i;
cout << "輸入不合法" << endl;
}
}
void InitChessBoard() //初始化棋盤
{
for (int i = 0; i < N + 1; ++i)
{
for (int j = 0; j < N + 1; ++j)
{
_ChessBoard[i][j] = ChessBoardflag;
}
}
}
void PrintChessBoard() //列印棋盤,這個函數可以自己調整
{
system("cls"); //系統調用,清空屏幕
for (int i = 0; i < N+1; ++i)
{
for (int j = 0; j < N+1; ++j)
{
if (i == 0) //列印列數字
{
if (j!=0)
printf("%d ", j);
else
printf(" ");
}
else if (j == 0) //列印行數字
printf("%2d ", i);
else
{
if (i < N+1)
{
printf("%c |",_ChessBoard[i][j]);
}
}
}
cout << endl;
cout << " ";
for (int m = 0; m < N; m++)
{
printf("--|");
}
cout << endl;
}
}
void PlayChess(Coordinate& pos, int player, int flag) //玩家下棋
{
PrintChessBoard(); //列印棋盤
while (1)
{
printf("玩家%d輸入坐標:", player);
cin >> pos.x >> pos.y;
if (JudgeValue(pos) == 1) //坐標合法
break;
cout << "坐標不合法,重新輸入" << endl;
}
_ChessBoard[pos.x][pos.y] = flag;
}
void ComputerChess(Coordinate& pos, char flag) //電腦下棋
{
PrintChessBoard(); //列印棋盤
int x = 0;
int y = 0;
while (1)
{
x = (rand() % N) + 1; //產生1~N的隨機數
srand((unsigned int) time(NULL));
y = (rand() % N) + 1; //產生1~N的隨機數
srand((unsigned int) time(NULL));
if (_ChessBoard[x][y] == ChessBoardflag) //如果這個位置是空的,也就是沒有棋子
break;
}
pos.x = x;
pos.y = y;
_ChessBoard[pos.x][pos.y] = flag;
}
int JudgeValue(const Coordinate& pos) //判斷輸入坐標是不是合法
{
if (pos.x > 0 && pos.x <= N&&pos.y > 0 && pos.y <= N)
{
if (_ChessBoard[pos.x][pos.y] == ChessBoardflag)
{
return 1; //合法
}
}
return 0; //非法
}
int JudgeVictory(Coordinate pos, char flag) //判斷有沒有人勝負(底層判斷)
{
int begin = 0;
int end = 0;
int begin1 = 0;
int end1 = 0;
//判斷行是否滿足條件
(pos.y - 4) > 0 ? begin = (pos.y - 4) : begin = 1;
(pos.y + 4) >N ? end = N : end = (pos.y + 4);
for (int i = pos.x, j = begin; j + 4 <= end; j++)
{
if (_ChessBoard[i][j] == flag&&_ChessBoard[i][j + 1] == flag&&
_ChessBoard[i][j + 2] == flag&&_ChessBoard[i][j + 3] == flag&&
_ChessBoard[i][j + 4] == flag)
return 1;
}
//判斷列是否滿足條件
(pos.x - 4) > 0 ? begin = (pos.x - 4) : begin = 1;
(pos.x + 4) > N ? end = N : end = (pos.x + 4);
for (int j = pos.y, i = begin; i + 4 <= end; i++)
{
if (_ChessBoard[i][j] == flag&&_ChessBoard[i + 1][j] == flag&&
_ChessBoard[i + 2][j] == flag&&_ChessBoard[i + 3][j] == flag&&
_ChessBoard[i + 4][j] == flag)
return 1;
}
int len = 0;
//判斷主對角線是否滿足條件
pos.x > pos.y ? len = pos.y - 1 : len = pos.x - 1;
if (len > 4)
len = 4;
begin = pos.x - len; //橫坐標的起始位置
begin1 = pos.y - len; //縱坐標的起始位置
pos.x > pos.y ? len = (N - pos.x) : len = (N - pos.y);
if (len>4)
len = 4;
end = pos.x + len; //橫坐標的結束位置
end1 = pos.y + len; //縱坐標的結束位置
for (int i = begin, j = begin1; (i + 4 <= end) && (j + 4 <= end1); ++i, ++j)
{
if (_ChessBoard[i][j] == flag&&_ChessBoard[i + 1][j + 1] == flag&&
_ChessBoard[i + 2][j + 2] == flag&&_ChessBoard[i + 3][j + 3] == flag&&
_ChessBoard[i + 4][j + 4] == flag)
return 1;
}
//判斷副對角線是否滿足條件
(pos.x - 1) >(N - pos.y) ? len = (N - pos.y) : len = pos.x - 1;
if (len > 4)
len = 4;
begin = pos.x - len; //橫坐標的起始位置
begin1 = pos.y + len; //縱坐標的起始位置
(N - pos.x) > (pos.y - 1) ? len = (pos.y - 1) : len = (N - pos.x);
if (len>4)
len = 4;
end = pos.x + len; //橫坐標的結束位置
end1 = pos.y - len; //縱坐標的結束位置
for (int i = begin, j = begin1; (i + 4 <= end) && (j - 4 >= end1); ++i, --j)
{
if (_ChessBoard[i][j] == flag&&_ChessBoard[i + 1][j - 1] == flag&&
_ChessBoard[i + 2][j - 2] == flag&&_ChessBoard[i + 3][j - 3] == flag&&
_ChessBoard[i + 4][j - 4] == flag)
return 1;
}
for (int i = 1; i < N + 1; ++i) //棋盤有沒有下滿
{
for (int j =1; j < N + 1; ++j)
{
if (_ChessBoard[i][j] == ChessBoardflag)
return 0; //0表示棋盤沒滿
}
}
return -1; //和棋
}
bool GetVictory(Coordinate& pos, int player, int flag) //對JudgeVictory的一層封裝,得到具體那個玩家獲勝
{
int n = JudgeVictory(pos, flag); //判斷有沒有人獲勝
if (n != 0) //有人獲勝,0表示沒有人獲勝
{
PrintChessBoard();
if (n == 1) //有玩家贏棋
{
if (player == 0) //0表示電腦獲勝,1表示玩家1,2表示玩家2
printf("***電腦獲勝*** ");
else
printf("***恭喜玩家%d獲勝*** ", player);
}
else
printf("***雙方和棋*** ");
return true; //已經有人獲勝
}
return false; //沒有人獲勝
}
private:
char _ChessBoard[N+1][N+1];
};
(1)c小游戲源碼擴展閱讀:
設計思路
1、進行問題分析與設計,計劃實現的功能為,開局選擇人機或雙人對戰,確定之後比賽開始。
2、比賽結束後初始化棋盤,詢問是否繼續比賽或退出,後續可加入復盤、悔棋等功能。
3、整個過程中,涉及到了棋子和棋盤兩種對象,同時要加上人機對弈時的AI對象,即涉及到三個對象。
『貳』 求用C語言編寫小游戲的源代碼,不管什麼小游戲,源代碼簡單些,適合初學者,3Q。
/*21點游戲*/
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int main(void )
{
int a,b,c,d,s,x,t,i,n,k;
s=200,k=1000;
x=t=i=1,a=b=c=d=n=0;
srand((int)time(0));
printf(" 21點游戲
★游戲規則: \n 游戲開始後系統將隨機產生1到10之間的數並累加.
1.在點數不大於21時,點數大者贏,點數相等算平局;
2.若一方點數大於21,一方點數小於21,小於21的一方贏;
3.若兩方均大於21,則算平局;
4.獲勝者獲得相應賭注金幣,金幣到零則結束游戲 .\n (注:繼續加點選1,不繼續加點選任意非1數)\n\n##########################\n\n") ;
do {
while(x==1)
{
if(t==1)
{
c=rand()%8+3;
a=rand()%10+1;
}
if(t==2)
{
a=rand()%5+1;
c=rand()%4+1;
}
if(t==3)
{
c=rand()%3+1;
a=rand()%3+1;
}
if(t==4)
{
a=rand()%2+1;
c=rand()%3+1;
}
if(t==5)
a=rand()%8+1;
if(t==6)
a=rand()%3+1;
b+=a,d+=c;
if(d>16)
t=2;
if(d==19&&b==19)
t=3;
if(d==20&&b==20)
t=3;
if(b==20&&d==19)
t=4;
if(d==19&&(d-b)>3)
t=5;
if(d==20&&d>b)
t=6;
if(d==21)
t=6;
if(i==1)
{
do
{
printf(" 您現在還有%d個金幣,系統有%d個,請輸入賭注: ",s,k);
scanf("%d",&n);
}
while(s<n);
i++;
}
if(b>21&&d<22)
{
printf(" 你%d點,系統%d點,你輸了!\n\n\n\n",b,d);
s-=n;
k+=n;
t=i=1,b=d=0;
break;
}
if(d>21&&b<22)
{
printf(" 你%d點,系統%d點,你贏了!\n\n\n\n",b,d);
s+=n;
k-=n;
t=i=1,b=d=0;
break;
}
if(d>21&&b>21)
{
printf(" 你%d點,系統%d點,平局!
\n\n\n",b,d);
t=i=1,b=d=0;
break;
}
printf(" 您加%d點,系統加%d點!\n 您現在共 %d點,系統現在共 %d點!\n 是否繼續加點 ",a,c,b,d);
scanf(" %d",&x);
a=c=0;
if(x!=1)
{
if(d<b&&d<18)
{
c=rand()%4+2;
d=d+c;
printf("您不加點,系統加%d點",c);
}
if(d<b&&d==20)
{
c=rand()%2+1;
d=d+c;
printf("您不加點,系統加%d點",c);
}
if(d<b&&d==19)
{
c=rand()%4+1;
d=d+c;
printf("您不加點,系統加%d點",c);
}
if(d<b&&d==18)
{
c=rand()%5+1;
d=d+c;
printf("您不加點,系統加%d點",c);
}
if(d>b)
printf("您不加點,系統不加點");
if(d<22&&d>b)
{
printf(" 你%d點,系統%d點,你輸了!\n\n\n\n",b,d);
s-=n;
k+=n;
t=x=i=1,b=d=0;
break;
}
if(b>d&&b<22||b<22&&d>21)
{
printf(" 你%d點,系統%d點,你贏了!\n\n\n\n",b,d);
s+=n;
k-=n;
t=x=i=1,b=d=0;
break;
}
if(b==d)
{
printf(" 你%d點,系統%d點,平局!
\n\n\n\n",b,d);
t=x=i=1,b=d=0;
break;
}
}
}
}
while(s>0&&k>0);
printf("您有%d個金幣,系統有%d個金幣!\n\n" ,s,k);
if(s>k)
printf("太棒了,你把系統打敗了!!!\n");else if(k>s)
printf(" 您的金幣不足,挑戰系統失敗!\n");
return 0;
}
『叄』 求一些C語言小游戲的源代碼,謝謝
「推箱子」C代碼:
#include <stdio.h>
#include <conio.h>
#include<stdlib.h>
#include<windows.h>
int m =0; //m代表第幾關
struct maps{short a[9][11]; };
struct maps map[5]={ 0,0,0,0,0,0,0,0,0,0,0, //共5關,每關9行11列
0,1,1,1,1,1,1,1,0,0,0,
0,1,0,0,0,0,0,1,1,1,0,
1,1,4,1,1,1,0,0,0,1,0, //0空地,1牆
1,5,0,0,4,0,0,4,0,1,0, //4是箱子,5是人
1,0,3,3,1,0,4,0,1,1,0, //3是目的地
1,1,3,3,1,0,0,0,1,0,0, //7是箱子在目的地(4+3)
0,1,1,1,1,1,1,1,1,0,0, //8是人在目的地(5+3)
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,1,1,1,1,0,0,0,0,0,
0,0,1,5,0,1,1,1,0,0,0,
0,0,1,0,4,0,0,1,0,0,0,
0,1,1,1,0,1,0,1,1,0,0,
0,1,3,1,0,1,0,0,1,0,0,
0,1,3,4,0,0,1,0,1,0,0,
0,1,3,0,0,0,4,0,1,0,0,
0,1,1,1,1,1,1,1,1,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,1,1,1,1,1,1,1,0,
0,0,1,1,0,0,1,0,5,1,0,
0,0,1,0,0,0,1,0,0,1,0,
0,0,1,4,0,4,0,4,0,1,0,
0,0,1,0,4,1,1,0,0,1,0,
1,1,1,0,4,0,1,0,1,1,0,
1,3,3,3,3,3,0,0,1,0,0,
1,1,1,1,1,1,1,1,1,0,0,
0,1,1,1,1,1,1,1,1,1,0,
0,1,0,0,1,1,0,0,0,1,0,
0,1,0,0,0,4,0,0,0,1,0,
0,1,4,0,1,1,1,0,4,1,0,
0,1,0,1,3,3,3,1,0,1,0,
1,1,0,1,3,3,3,1,0,1,1,
1,0,4,0,0,4,0,0,4,0,1,
1,0,0,0,0,0,1,0,5,0,1,
1,1,1,1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,1,1,1,1,1,1,0,0,
0,1,1,1,0,0,0,0,1,0,0,
1,1,3,0,4,1,1,0,1,1,0,
1,3,3,4,0,4,0,0,5,1,0,
1,3,3,0,4,0,4,0,1,1,0,
1,1,1,1,1,1,0,0,1,0,0,
0,0,0,0,0,1,1,1,1,0,0,
0,0,0,0,0,0,0,0,0,0,0 };
void DrMap( ) //繪制地圖
{ CONSOLE_CURSOR_INFO cursor_info={1,0}; //隱藏游標的設置
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
printf(" 推箱子");
printf(" ");
for (int i = 0; i < 9; i++)
{for (int j = 0; j < 11; j++)
{switch (map[m].a[i][j])
{case 0: printf(" "); break;
case 1: printf("■"); break;
case 3: printf("◎");break;
case 4: printf("□"); break;
case 5: printf("♀"); break; //5是人
case 7: printf("□"); break; //4 + 3箱子在目的地中
case 8: printf("♀");break; // 5 + 3人在目的地中
}
}
printf(" ");
}
}
void gtxy(int x, int y) //控制游標位置的函數
{ COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void start( ) //開始游戲
{ int r, c; //人的下標
for (int i = 0; i < 9; i++)
{ for (int j = 0; j < 11; j++)
{if (map[m].a[i][j] == 5||map[m].a[i][j]==8) { r = i; c = j; } } //i j 人的下標
}
char key;
key = getch( );
switch (key)
{case 'W':
case 'w':
case 72:
if (map[m]. a[r - 1][c] == 0|| map[m]. a [r - 1][c] == 3)
{ gtxy(2*c+8,r-1+3); printf("♀"); // gtxy(2*c+8,r-1+3)是到指定位置輸出字元
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r - 1][c] += 5; map[m]. a [r][c] -= 5; }
else if (map[m]. a [r - 1][c] == 4 || map[m]. a [r - 1][c] == 7)
{ if (map[m]. a [r - 2][c] == 0 || map[m]. a [r - 2][c] == 3)
{ gtxy(2*c+8,r-2+3); printf("□"); gtxy(2*c+8,r-1+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r - 2][c] += 4; map[m]. a [r - 1][c] += 1;
map[m]. a [r][c] -= 5; }
} break;
case 'S':
case 's':
case 80:
if (map[m]. a [r + 1][c] == 0 || map[m]. a [r + 1][c] == 3)
{ gtxy(2*c+8,r+1+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r + 1][c] += 5; map[m]. a [r][c] -= 5; }
else if (map[m]. a [r + 1][c] == 4 || map[m]. a [r+ 1][c] == 7)
{ if (map[m]. a [r + 2][c] == 0 || map[m]. a [r + 2][c] == 3)
{ gtxy(2*c+8,r+2+3); printf("□"); gtxy(2*c+8,r+1+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r + 2][c] += 4; map[m]. a [r + 1][c] += 1;
map[m]. a [r][c] -= 5; }
}break;
case 'A':
case 'a':
case 75:
if (map[m]. a [r ][c - 1] == 0 || map[m]. a [r ][c - 1] == 3)
{ gtxy(2*(c-1)+8,r+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r ][c - 1] += 5; map[m]. a [r][c] -= 5; }
else if (map[m]. a [r][c - 1] == 4 || map[m]. a [r][c - 1] == 7)
{if (map[m]. a [r ][c - 2] == 0 || map[m]. a [r ][c - 2] == 3)
{ gtxy(2*(c-2)+8,r+3); printf("□"); gtxy(2*(c-1)+8,r+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r ][c - 2] += 4; map[m]. a [r ][c - 1] += 1;
map[m]. a [r][c] -= 5; }
}break;
case 'D':
case 'd':
case 77:
if (map[m]. a [r][c + 1] == 0 || map[m]. a [r][c + 1] == 3)
{ gtxy(2*(c+1)+8,r+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8) {gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r][c + 1] += 5; map[m]. a [r][c] -= 5; }
else if (map[m]. a [r][c + 1] == 4 || map[m]. a [r][c + 1] == 7)
{ if (map[m]. a [r][c + 2] == 0 || map[m]. a [r][c + 2] == 3)
{ gtxy(2*(c+2)+8,r+3); printf("□"); gtxy(2*(c+1)+8,r+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r][c + 2] += 4; map[m]. a [r][c + 1] += 1;
map[m]. a [r][c] -= 5; }
}break;
}
}
int ifwan( ) //是否完成(1是0否)
{ if(m==0){if(map[m].a[5][2]==7&& map[m].a[5][3]==7&&
map[m].a[6][2]==7&& map[m].a[6][3]==7) return 1;}
if(m==1){if(map[m].a[5][2]==7&& map[m].a[6][2]==7&&
map[m].a[7][2]==7) return 1;}
if(m==2){if(map[m].a[7][1]==7&& map[m].a[7][2]==7&& map[m].a[7][3]==7&&
map[m].a[7][4]==7&& map[m].a[7][5]==7) return 1;}
if(m==3){if(map[m].a[4][4]==7&& map[m].a[4][5]==7&& map[m].a[4][6]==7&&
map[m].a[5][4]==7&& map[m].a[5][5]==7&& map[m].a[5][6]==7) return 1;}
if(m==4){if(map[m].a[3][2]==7&& map[m].a[4][1]==7&& map[m].a[4][2]==7&&
map[m].a[5][1]==7&& map[m].a[5][2]==7) return 1;}
return 0;
}
int main( ) //主函數
{ while (1)
{ system("cls");
DrMap( );
while (1)
{ start( );
if(ifwan()){printf("