c/c++开发分享C语言实现三子棋小游戏(vs2013多文件)通过实例为大家分享了c语言实现三子棋小游戏的具体代码,供大家参考,具体内容如下
基本思路:
1.创建一个游戏选择面板.
2.创建并初始化棋盘。
3.玩家落子并判定,电脑落子并判定。
4.判定结果 ,游戏结束!
代码如下:
头文件:
#pragma once #include<stdio.h> #include<windows.h> #include<stdlib.h> #include <time.h> #pragma warning(disable:4996) extern void game();//游戏代码需要定义调用的函数 #define row 3//fow、fol代表三子棋边界的长和宽 #define col 3 #define init ' ' //init为空格的宏定义 #define white 'x' //x在三字棋代表player #define black 'o' //o在三字棋代表computer #define draw 'd' //draw代表平局 #define next 'n' //next代表继续 //mineclearence
各类函数:
void initboard(char board[][col], int row, int col)//棋盘初始化 { for (int i = 0; i < row; i++){ for (int j = 0; j < col; j++){ board[i][j] = init; } } } static void showboard(char board[][col], int row, int col)//显示棋盘 { system("cls"); printf(" "); for (int i = 0; i < col; i++){ printf("%4d", i + 1); } printf("n--------------n"); for (int i = 0; i < row; i++){ printf("%-2d", i + 1); //2 for (int j = 0; j < col; j++){ printf("| %c ", board[i][j]); } printf("n--------------n"); } } static char isend(char board[][col], int row, int col)//最终结果 { for (int i = 0; i < row; i++){ if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != init){ return board[i][0]; } } for (int j = 0; j < col; j++){ if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != init){ return board[0][j]; } } if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != init){ return board[1][1]; } if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != init){ return board[1][1]; } for (int i = 0; i < row; i++){ for (int j = 0; j < col; j++){ if (board[i][j] == init){ return next; } } } return draw; } static void playermove(char board[][col], int row, int col)//玩家 { int x = 0; int y = 0; while (1){ printf("please enter postion<x,y># "); scanf("%d %d", &x, &y); if (x < 1 || y < 1 || x > 3 || y > 3){ printf("enter postion error!n"); continue; } if (board[x - 1][y - 1] == init){ board[x - 1][y - 1] = white; break; } else{ printf("postion is not empty!n"); } } } static void computermove(char board[][col], int row, int col)//电脑 { while (1){ int x = rand() % row; int y = rand() % col; if (board[x][y] == init){ board[x][y] = black; break; } } } void game() { char board[row][col]; initboard(board, row, col); srand((unsigned long)time(null)); char result = 0; while (1){ showboard(board, row,col); playermove(board, row,col); result = isend(board, row, col); if (result != next){ break; } showboard(board, row, col); computermove(board,row, col); result = isend(board, row,col); if (result != next){ break; } } showboard(board, row,col); switch (result){ case white: printf("you win!n"); break; case black: printf("you lose!n"); break; case draw: printf("you == computer!n"); break; default: printf("bug!n"); break; } }
游戏选择面板(主函数)
#include "game.h" static void menu() { printf("|------------- 开始游戏 -------------|n"); printf("+------------- 1. play -------------+n"); printf("+------------- 0. exit -------------+n"); } int main() { int select = 0; int quit = 0; while (!quit){ menu(); printf("please select#"); scanf("%d",&select); switch (select){ case 1: game(); break; case 0: quit = 1; break; default: printf("enter error, try again!n"); break; } } printf("bye!n"); system("pause"); return 0; }
以上就是c/c++开发分享C语言实现三子棋小游戏(vs2013多文件)的全部内容,希望对大家的学习有所帮助,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。
需要了解更多c/c++开发分享C语言实现三子棋小游戏(vs2013多文件),都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/637900.html