又出bug 心累
#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>#include"game.h"void menu(){ printf("**********************************************\n"); printf("*********** 1.play 0.exit *************\n"); printf("**********************************************\n");}void game()//游戏的整个实现{ //数组-存放玩家和电脑走出的棋盘信息 char board[ROW][COL] = {' '};//全部为空格 //初始化棋盘-数组 InitBoard(board, ROW, COL); //打印棋盘 DisplayBoard(board, ROW, COL); //下棋 while (1) { //玩家下棋 PlayerMove(board, ROW, COL); DisplayBoard(board, ROW, COL); //电脑下棋 //ComputerMove(); }}void test()//游戏流程步骤{ int input = 0; do { menu(); printf("请选择:>"); scanf("%d", &input); switch (input) { case 1: printf("三子棋\n"); game(); break; case 0: printf("退出游戏\n"); break; default: printf("选择错误,请重新选择!\n"); break; } } while (input);}int main(){ test();//写游戏 return 0;}
#pragma once#include<stdio.h>#define _CRT_SECURE_NO_WARNINGS 1#define ROW 3#define COL 3void InitBoard(char board[ROW][COL], int row, int col);void DisplayBoard(char board[ROW][COL], int row, int col);void PlayerMove(char board[ROW][COL], int row, int col);
#define _CRT_SECURE_NO_WARNINGS 1#include"game.h"void InitBoard(int board[ROW][COL], int row, int col){ int i = 0; int j = 0; while (i < row) { while (j < col) { board[i][j] = ' '; j++; } i++; }}void DisplayBoard(char board[ROW][COL], int row, int col){ int i = 0; for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { //1.打印一行的数据 printf(" %c ", board[i][j]); if (j < col - 1) { printf("|"); } } printf("\n"); if (i < row - 1) { for (j = 0; j < col; j++) { printf("---"); if (j < col - 1) printf("|"); } printf("\n"); } //2.打印分隔行 }}void PlayerMove(char board[ROW][COL], int row, int col){ int x = 0; int y = 0; printf("玩家走:>\n"); while (1) { printf("请输入要下的坐标:>"); scanf("%d%d", &x, &y); //判断坐标的合法性 if (x >= 1 && x <= row && y >= 1 && y <= col) { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*'; break; } else { printf("该坐标被占用\n"); } } else { printf("坐标非法,请重新输入\n"); } }}