c++编写井字棋小游戏代码

时间: 2023-09-24 11:13:42 浏览: 93
下面是一个简单的井字棋小游戏的 C++ 代码,希望能帮到你: ```c++ #include <iostream> using namespace std; char square[10] = {'0','1','2','3','4','5','6','7','8','9'}; int checkwin(); void board(); int main() { int player = 1,i,choice; char mark; do { board(); player=(player%2)?1:2; cout << "Player " << player << ", enter a number: "; cin >> choice; mark=(player == 1) ? 'X' : 'O'; if (choice == 1 && square[1] == '1') square[1] = mark; else if (choice == 2 && square[2] == '2') square[2] = mark; else if (choice == 3 && square[3] == '3') square[3] = mark; else if (choice == 4 && square[4] == '4') square[4] = mark; else if (choice == 5 && square[5] == '5') square[5] = mark; else if (choice == 6 && square[6] == '6') square[6] = mark; else if (choice == 7 && square[7] == '7') square[7] = mark; else if (choice == 8 && square[8] == '8') square[8] = mark; else if (choice == 9 && square[9] == '9') square[9] = mark; else { cout<<"Invalid move "; player--; cin.ignore(); cin.get(); } i=checkwin(); player++; }while(i==-1); board(); if(i==1) cout<<"==>\aPlayer "<<--player<<" win "; else cout<<"==>\aGame draw"; cin.ignore(); cin.get(); return 0; } int checkwin() { if (square[1] == square[2] && square[2] == square[3]) return 1; else if (square[4] == square[5] && square[5] == square[6]) return 1; else if (square[7] == square[8] && square[8] == square[9]) return 1; else if (square[1] == square[4] && square[4] == square[7]) return 1; else if (square[2] == square[5] && square[5] == square[8]) return 1; else if (square[3] == square[6] && square[6] == square[9]) return 1; else if (square[1] == square[5] && square[5] == square[9]) return 1; else if (square[3] == square[5] && square[5] == square[7]) return 1; else if (square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4' && square[5] != '5' && square[6] != '6' && square[7] != '7' && square[8] != '8' && square[9] != '9') return 0; else return -1; } void board() { system("cls"); cout << "\n\n\tTic Tac Toe\n\n"; cout << "Player 1 (X) - Player 2 (O)" << endl << endl; cout << endl; cout << " | | " << endl; cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl; cout << "_____|_____|_____" << endl; cout << " | | " << endl; cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl; cout << "_____|_____|_____" << endl; cout << " | | " << endl; cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl; cout << " | | " << endl << endl; } ``` 这个代码中,我们使用了一个字符数组来存储井字棋的状态,每次玩家输入一个数字代表着玩家在棋盘上落子,程序会判断输入是否合法,如果合法,将玩家指定的字符存储到字符数组中。每次落子后,程序调用 checkwin() 函数来判断是否有玩家胜利或平局,如果有,游戏结束,输出结果。如果没有,继续进行下一轮游戏。

相关推荐

最新推荐

recommend-type

C++面向对象实现五子棋小游戏

本文介绍了如何运用面向对象思想进行五子棋游戏的设计与开发,与面向过程程序设计比较,面向对象程序设计更易于实现对现实世界的描述,提高软件的扩展性和可维护性。附上最终的程序源码,推荐给大家,有需要的小伙伴...
recommend-type

基于c++ ege图形库实现五子棋游戏

主要为大家详细介绍了基于c++ ege图形库实现五子棋游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

C++基于EasyX图形库实现2048小游戏

主要为大家详细介绍了C++基于EasyX图形库实现2048小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

使用c++编写和使用.so动态链接库

1,使用 c 生成动态链接库mylib.so的简单示例  声明文件mylib.h   #ifndef __MY_LIB_H__ #define __MY_LIB_H__ void foobar(int i);... #endif /* __MY_LIB_H__ */ ... printf("Printing ...
recommend-type

C++实现数字转换为十六进制字符串的方法

主要介绍了C++实现数字转换为十六进制字符串的方法,涉及C++操作数字与字符串转换的相关技巧,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

SPDK_NVMF_DISCOVERY_NQN是什么 有什么作用

SPDK_NVMF_DISCOVERY_NQN 是 SPDK (Storage Performance Development Kit) 中用于查询 NVMf (Non-Volatile Memory express over Fabrics) 存储设备名称的协议。NVMf 是一种基于网络的存储协议,可用于连接远程非易失性内存存储器。 SPDK_NVMF_DISCOVERY_NQN 的作用是让存储应用程序能够通过 SPDK 查询 NVMf 存储设备的名称,以便能够访问这些存储设备。通过查询 NVMf 存储设备名称,存储应用程序可以获取必要的信息,例如存储设备的IP地址、端口号、名称等,以便能
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。