Unity3d 游戏设计(一)井字棋
3D游戏设计(一)井字棋
运行效果:

实现过程
声明变量:
public Texture2D O;
public Texture2D X;
GUIStyle myStyle;
private int turn = 1;
/*记录谁的回合:turn=1为,turn==2为×*/
int[][] board = new int[3][] { new int[3], new int[3], new int[3] };
reset()函数:用于重新开始游戏
/*重新开局*/
void reset() {
turn = 1;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
board[i][j] = 0;
}
}
Debug.Log("game reset");
}
check()函数:检查游戏是否结束:未结束返回0,O胜利返回1,X胜利返回2,平局返回3。
/*检查胜利条件*/
int check() {
/*横线获胜*/
for (int i = 0; i < 3; i++) {
if (board[i][0] != 0 && board[i][0] == board[i][1] && board[i][1] == board[i][2]) {
return board[i][0];
}
}
/*纵线获胜*/
for (int i = 0; i < 3; i++) {
if (board[0][i] != 0 && board[0][i] == board[1][i] && board[1][i] == board[2][i]) {
return board[0][i];
}
}
/*斜线获胜*/
if (board[1][1] != 0 &&
board[0][0]== board[1][1] && board[2][2] == board[1][1] ||
board[0][2] == board[1][1] && board[2][0] == board[1][1]) {
return board[1][1];
}
/*对局还没结束*/
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (board[i][j] == 0) return 0;
}
}
/*平局*/
return 3;
}
display_result()函数:显示目前的游戏状态。
/*显示对局情况*/
void display_result(int res) {
int m = Screen.width / 2;
if (res == 0) {
if (turn == 1)
GUI.Box(new Rect(m - 50, 115, 100, 35), "O is going");
else
GUI.Box(new Rect(m - 50, 115, 100, 35), "X is going");
}
else if (res == 1) {
GUI.Box(new Rect(m - 50, 115, 100, 35), "O WIN");
}
else if (res == 2) {
GUI.Box(new Rect(m - 50, 115, 100, 35), "X WIN");
}
else if (res == 3) {
GUI.Box(new Rect(m - 50, 115, 100, 35), "Draw");
}
}
OnGUI()函数:实时渲染,采用预设的O、X图片来表示O、X已经下过的棋子。
void OnGUI() {
int m = Screen.width / 2;
GUI.Box(new Rect(m - 150, 80, 300, 420), "井字棋");
/*如果reset被点击则重开*/
if (GUI.Button(new Rect(m - 50, 160, 100, 35), new GUIContent("Reset", "点击重新开始"))) reset();
GUI.Label(new Rect(m - 150, 170, 150, 35), GUI.tooltip);
int res = check();
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (board[i][j] == 1) {
GUI.Button(new Rect(m - 150 + i * 100, 200 + j * 100, 100, 100), O);
}
else if (board[i][j] == 2) {
GUI.Button(new Rect(m - 150 + i * 100, 200 + j * 100, 100, 100), X);
}
if(GUI.Button(new Rect(m - 150 + i * 100, 200 + j * 100, 100, 100), "")) {
if (res == 0) {
if (turn == 1) {
board[i][j] = turn;
turn = 2;
}
else {
board[i][j] = turn;
turn = 1;
}
}
}
}
}
display_result(res);
}
可能存在的问题及解决方案:
可能存在井字棋显示不全的问题,此时把视野调至标准的1024*768即可。
Unity3d 游戏设计(一)井字棋的更多相关文章
- JavaFX 井字棋游戏
利用JavaFX设计一个井字棋游戏,其中包括了能够与玩家对战的AI.AI的实现相比五子棋来说要简单得多,可以保证AI在后手情况下绝对不会输,具体实现如下: /* * To change this li ...
- [LeetCode] 348. Design Tic-Tac-Toe 设计井字棋游戏
Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume the fol ...
- 井字棋游戏升级版 - TopTicTacToe项目 简介
一.游戏简介 井字棋是一款世界闻名的游戏,不用我说,你一定知道它的游戏规则. 这款游戏简单易学,玩起来很有意思,不过已经证明出这款游戏如果两个玩家都足够聪明的话, 是很容易无法分出胜负的,即我们得到的 ...
- [CareerCup] 17.2 Tic Tac Toe 井字棋游戏
17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...
- [C++] 井字棋游戏源码
TicTac.h #define EX 1 //该点左鼠标 #define OH 2 //该点右鼠标 class CMyApp : public CWinApp { public: virtual B ...
- [游戏学习22] MFC 井字棋 双人对战
>_<:太多啦,感觉用英语说的太慢啦,没想到一年做的东西竟然这么多.....接下来要加速啦! >_<:注意这里必须用MFC和前面的Win32不一样啦! >_<:这也 ...
- [HTML5实现人工智能]小游戏《井字棋》发布,据说IQ上200才能赢
一,什么是TicTacToe(井字棋) 本 游戏 为在下用lufylegend开发的第二款小游戏.此游戏是大家想必大家小时候都玩过,因为玩它很简单,只需要一张草稿纸和一只笔就能开始游戏,所以广受儿 ...
- python 游戏(井字棋)
1. 游戏思路和流程图 实现功能,现实生活中的井字棋玩法 游戏流程图 2. 使用模块和游戏提示 import random def game_info(): print('欢迎来到井字棋游戏') pr ...
- 『HTML5实现人工智能』小游戏《井字棋》发布,据说IQ上200才能赢【算法&代码讲解+资源打包下载】
一,什么是TicTacToe(井字棋) 本游戏为在下用lufylegend开发的第二款小游戏.此游戏是大家想必大家小时候都玩过,因为玩它很简单,只需要一张草稿纸和一只笔就能开始游戏,所以广受儿童欢迎. ...
随机推荐
- 10.oracle分页
oracle的分页一共有三种方式 方法一 根据rowid来分 SELECT * FROM EMP WHERE ROWID IN (SELECT RID FROM (SELECT ROWNUM RN, ...
- node.js03 第一个node.js程序和读取文件
Hello World 1.创建运行 创建txt文件起名为hellonode,在记事本中编写JavaScript脚本文件 例如: var bbl = 'hellonode' console.log(b ...
- ceph osd跟cpu进行绑定
通过cgroup将ceph-osd进程与某一个 CPU core 绑定脚本: mkdir -p /sys/fs/cgroup/cpuset/ceph # cup number : ,,, = - ec ...
- 自我介绍网页填写表格PHP,JavaScript,html,css代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Python的pyttsx3安装失败的解决方案
尝试更新安装工具,然后重试安装: pip install -U setuptools pip install pyttsx3 如果仍不能解决您的问题,您也可以尝试指定pyttsx3的版本 pip in ...
- idea 推送jar包到maven私库
1.推送到本地仓库 idea直接install即可 2.推送到公司私库 2.1 使用配置公司仓库地址的setting文件 <?xml version="1.0" encodi ...
- Shell编程—数据展示
1.标准文件描述符 Linux用文件描述符(file descriptor)来标识每个文件对象.文件描述符是一个非负整数,可以唯一标识会话中打开的文件.每个进程一次 多可以有九个文件描述符.出于特殊目 ...
- react+antd的todolist开发
使用localStorage缓存在cookie里刷新不会充重置 参考链接 create-react-app入门教程https://www.jianshu.com/p/77bf3944b0f4 http ...
- slua中,绑定lua文件到Monobehavior的一种方法
slua本身并不提供如何把一个lua文件绑定到一个预制中,就像一个普通的继承自monobehavior的自定义脚本那样,而tolua的框架却采用了拙劣的做法: public class LuaBeha ...
- Activiti7 查询用户任务列表
package com.itheima.activiti; import org.activiti.engine.ProcessEngine; import org.activiti.engine.P ...
