c语言程序设计扑克牌游戏?
定义一个结构类型表示一张牌,结构包含3个成员,第一个成员char:取值2,3~K,A表示牌名字,第二个成员int:取值2~14表示牌真实大小。第三个成员:结构链表指针。
写一个初始化函数,定义52大小的结构数组,成员值初值分别和牌对应,遍历数组并将每个元素的链表指针依次指向下一个元素地址。这样得到一个初始链表。(相当于一盒新牌)
所有涉及随机数都用rand函数,洗牌分四份就是循环取随机数m=1~n,n是随循环自减,初值52,直到n变成0。每随一次循环就从初始链表中遍历取出对应第m个节点,并从初始链表中将这个节点断开(既前一个节点指针直接指向后一个节点指针)。每取13张就组成一个新的链表。这样获得4个新链表分别表示4个玩家。
最后出牌就是分别遍历自己的链表,利用循环取牌比较结构数值大小。(取出的牌要从链表断开和上面一样,你把取出节点写成独立函数就能反复使用)。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int i,m,n
srand(time(NULL))
m=rand()%10+1
for(i=0i<3++i)
{
scanf("%d",&n)
if(m==n)
break
}
switch(i)
{
case 0:puts("优秀")break
case 1:puts("良好")break
case 2:puts("合格")break
case 3:puts("不合格")break
}
return 0
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int x, magic, a = 0, c = 1
char b
do
{
srand(unsigned(time(NULL))) //为函数rand()设置随机数种子
magic = rand() % 100 + 1 //magic为1~100间的一个数
printf("您每次游戏中,只有十次机会\n")
while (a <= 10)
{
scanf("%d", &x)
if (x>magic)
printf("Wrong,比magic大\n")
else if (x<magic)
printf("Wrong,比magic小\n")
//else if (x = magic) //这里的逻辑运算应该使用“==”
else if (x == magic) //你应该是写错了
{
printf("Right\n")
a = a + 1
printf("你猜了%d次了\n", a)
a = 0 //猜对之后,应该对猜测的次数初始化
break
}
a++
printf("你猜了%d次了\n", a)
if (a == 10)
{
a = 0
printf("Do you want to continue?(回答Y/N或者y/n)\n")
//scanf_s("%c", &b) //这么使用,会将你在输入字符之前的
// 空行(enter键)赋值给 b,而不是你输入的Y/N/y/n
scanf(" %c", &b) //而这种写法,%c 前面的 空格会使 scanf 忽略前面的 enter键
//等待第一个非 enter键 元素读入
if (b == 'y' || b == 'Y')
c = 1
else if (b == 'n' || b == 'N')
c = 0
break //当猜测次数为 10 时,应当跳出
//这里不会自动跳出,因为 a 被初始化为 0
}
}
} while (c == 1)
return 0
}
scanf 里面没有空格的情况,程序会自动执行 10 次
修改后的程序,猜对的情况
修改后的程序,猜错 10 次,Y的情况
修改后的程序,猜错 10 次,N的情况
可以用C语言编写游戏的。
C语言是一门通用计算机编程语言,应用广泛。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。
尽管C语言提供了许多低级处理的功能,但仍然保持着良好跨平台的特性,以一个标准规格写出的C语言程序可在许多电脑平台上进行编译,甚至包含一些嵌入式处理器(单片机或称MCU)以及超级电脑等作业平台。
#include <windows.h>
#include <stdlib.h>
#include <time.h>
const int LENGTH = 40
const int WIDTH = 10
const int RANGE = 50
const int BeginLength = 5
const int speed = 300
#define SNAKE_COLOR RGB(176, 196, 222)
#define BACKGROUND_COLOR RGB(255, 255, 0)
#define DRAW_SNAKE(x) (x) * WIDTH
enum IsSnake {isSnake, isNotSnake, isFood}
IsSnake map[LENGTH][LENGTH]
struct snake {
int x
int y
snake* next
snake(int x, int y, snake* n = NULL) {
this -> x = x
this -> y = y
next = n
}
} // snake
typedef struct snake Snake
Snake* head = NULL // queue
Snake* tail = NULL // queue
int direct = 0 // direction
RECT playground // district
TCHAR szAppName[] = TEXT("-*- snake game -* ") // The project name
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM) // message function
void Initializer()
void Controller(Snake*,LPVOID) // control the snake
void Move(HWND)
void PutFood()
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, PSTR szCmdLine, int iCmdShow) {
MSG msg
HWND hwnd
WNDCLASS wndclass
while (TRUE) {
wndclass.cbClsExtra = 0
wndclass.cbWndExtra = 0
wndclass.hbrBackground = CreateSolidBrush(RGB(255, 255, 255))
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW)
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION)
wndclass.hInstance = hInstance
wndclass.lpfnWndProc = WndProc
wndclass.lpszClassName = szAppName
wndclass.lpszMenuName = NULL
wndclass.style = CS_HREDRAW | CS_VREDRAW
if (!RegisterClass(&wndclass)) {
MessageBox(NULL, TEXT("Please try again!!!"), szAppName, MB_ICONERROR)
return 0
}
break
}
hwnd = CreateWindow(szAppName, TEXT("<^_^> Snake Game <^_^>"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL)
ShowWindow(hwnd, SW_NORMAL)
UpdateWindow(hwnd)
while (TRUE) {
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT)
break
TranslateMessage(&msg)
DispatchMessage(&msg)
} else {
Move(hwnd)
}
}
return msg.wParam
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
HDC hdc
PAINTSTRUCT ps
HBRUSH hBrush
switch (message) {
case WM_DESTROY:
PostQuitMessage(0)
return 0
case WM_CREATE:
Initializer()
MoveWindow(hwnd, RANGE, RANGE, WIDTH * LENGTH + RANGE * 3, WIDTH * LENGTH + RANGE * 3, TRUE)
return 0
case WM_KEYDOWN:
switch (wParam) {
case VK_LEFT:
if (direct != VK_RIGHT)
direct = VK_LEFT
break
case VK_RIGHT:
if (direct != VK_LEFT)
direct = VK_RIGHT
break
case VK_UP:
if (direct != VK_DOWN)
direct = VK_UP
break
case VK_DOWN:
if (direct != VK_UP)
direct = VK_DOWN
break
default:
break
}
return 0
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps)
SetViewportOrgEx(hdc, RANGE, RANGE, NULL)
hBrush = CreateSolidBrush(BACKGROUND_COLOR)
SelectObject(hdc, hBrush)
Rectangle(hdc,playground.left, playground.top, playground.right, playground.bottom)
DeleteObject(hBrush)
hBrush = CreateSolidBrush(SNAKE_COLOR)
SelectObject(hdc,hBrush)
for (int i = 0 i < LENGTH ++i) {
for (int j = 0 j < LENGTH ++j) {
if (map[i][j] == isSnake || map[i][j] == isFood) {
Rectangle(hdc, DRAW_SNAKE(i), DRAW_SNAKE(j), DRAW_SNAKE(i + 1), DRAW_SNAKE(j + 1))
}
}
}
DeleteObject(hBrush)
EndPaint(hwnd, &ps)
}
return DefWindowProc(hwnd, message, wParam, lParam)
}
void Initializer() {
for (int i = 0 i < LENGTH ++i) {
for (int j = 0 j < LENGTH ++j) {
map[i][j] = isNotSnake
}
}
for (i = 0 i < BeginLength ++i) {
if (i == 0) {
head = tail = new snake(i, 0)
} else {
snake* temp = new snake(i, 0)
tail -> next = temp
tail = temp
}
map[i][0] = isSnake
}
playground.left = playground.top = 0
playground.right = playground.bottom = WIDTH * LENGTH
direct = VK_RIGHT
PutFood()
}
void PutFood() {
srand(static_cast<unsigned>(time(NULL)))
int x, y
do {
x = rand() % LENGTH
y = rand() % LENGTH
} while (map[x][y] == isSnake)
map[x][y] = isFood
} // put food
void Move(HWND hwnd) {
snake* temp
switch (direct) {
case VK_LEFT:
temp = new snake(tail -> x - 1, tail -> y)
break
case VK_RIGHT:
temp = new snake(tail -> x + 1, tail -> y)
break
case VK_UP:
temp = new snake(tail -> x, tail -> y - 1)
break
case VK_DOWN:
temp = new snake(tail -> x, tail -> y + 1)
break
}
Controller(temp,hwnd)
//InvalidateRect(hwnd,NULL,FALSE)
Sleep(speed) // control speed
} // snake moving
void Controller(Snake* temp,LPVOID lParam) {
HWND hwnd
hwnd=(HWND)lParam
if (temp -> x < 0 || temp -> x >= LENGTH || temp -> y < 0 || temp -> y >= LENGTH
|| map[temp -> x][temp -> y] == isSnake) { // the snake is died
MessageBox(NULL,TEXT("<Copyright liaoqb> Sorry !!! Game Over !!! <Copyright liaoqb>"),szAppName,0)
delete temp
while (head != NULL) {
Snake* ptr = head
head = head -> next
delete ptr
}
head = tail = temp = NULL
Initializer()
return
} else if (map[temp -> x][temp -> y] == isNotSnake) { // move
map[temp -> x][temp -> y] = isSnake
map[head -> x][head -> y] = isNotSnake
snake* ptr = head
head = head -> next
delete ptr
tail -> next = temp
tail = temp
InvalidateRect(hwnd,NULL,FALSE)
} else { // if eat food
map[temp -> x][temp -> y] = isSnake
tail -> next = temp
tail = temp
PutFood()
//InvalidateRect(hwnd,NULL,FALSE)
}
}
可以用递归来做,
假设 有A,B两堆石子。 A的数量是x,B的是y
递归的出口是3个状态。
1:其一等于1,另一个等于2 (输)
2:其一等于1,另一个>2 (赢)
3:其一等于2,另一个>1 (赢)
另外,只需要定义操作了, 操作只能是两者之一。 其一:(de_both)两堆都减去同一数字的石子。另外一个(de_one)就是人选一堆,拿掉任意个数的石子。
递归过程如下;
void simulate(int a,int b){
switch(state)
{
case 1:
you lose
case 2:
break
case 3:
you win
}
if(abs(a,b)=1)
{
de_both(min(a,b)-1)
}
else
{
de_one(random)
}
simulate(a,b)
}