Android --#字XO小游戏
Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:id="@+id/titleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/title" /> <Button
android:id="@+id/newGameBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="newGame"
android:text="New Game" /> <TableLayout
android:id="@+id/tableLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" > <TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <Button
android:id="@+id/topLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:height="100dp"
android:text="O"
android:width="100dp" /> <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:height="100dp"
android:text="O"
android:width="100dp" /> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:height="100dp"
android:text="O"
android:width="100dp" />
</TableRow> <TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:height="100dp"
android:text="O"
android:width="100dp" /> <Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:height="100dp"
android:text="O"
android:width="100dp" /> <Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:height="100dp"
android:text="O"
android:width="100dp" />
</TableRow> <TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:height="100dp"
android:text="O"
android:width="100dp" /> <Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:height="100dp"
android:text="O"
android:width="100dp" /> <Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:height="100dp"
android:text="O"
android:width="100dp" />
</TableRow>
</TableLayout> </LinearLayout>
Java
package com.example.NaughtsAndCrosses; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView; public class MainActivity extends Activity { /**
* 轮到了正方还是反方,默认为X
*/
private boolean noughtsTurn = false; /**
* 用于记录表格中的数据
*/
private char board[][] = new char [ 3 ] [ 3 ]; @ Override
public void onCreate ( Bundle savedInstanceState ) { super.onCreate ( savedInstanceState );
setContentView ( R.layout.main ); setupOnClickListeners ( ); resetButtons ( );
} /**
* 当点击new Game按钮时候的点击事件 --> 初始化数据
*/
public void newGame ( View view ) { noughtsTurn = false;
board = new char [ 3 ] [ 3 ];
resetButtons ( );
} /**
* 重置每个按钮的颜色以及文字
*/
private void resetButtons ( ) { // 找到TableLayout组件
TableLayout T = ( TableLayout ) findViewById ( R.id.tableLayout ); /**
* TabLayout --> TabRow --> Button
*/
for ( int y = 0 ; y < T.getChildCount ( ) ; y ++ ) { if (T.getChildAt ( y ) instanceof TableRow) { TableRow R = ( TableRow ) T.getChildAt ( y ); for ( int x = 0 ; x < R.getChildCount ( ) ; x ++ ) { if (R.getChildAt ( x ) instanceof Button) { Button B = ( Button ) R.getChildAt ( x );
B.setText ( "" );
B.setEnabled ( true );
}
}
}
} // 找到文字组件并 设置提示的文字信息
TextView t = ( TextView ) findViewById ( R.id.titleText );
t.setText ( R.string.title ); } /**
* 检查是否有人获胜
*/
private boolean checkWin ( ) { // 获胜方为X
char winner = '\0';
if (checkWinner ( board , 3 , 'X' )) {
winner = 'X';
}
// 获胜方为Y
else if (checkWinner ( board , 3 , 'O' )) {
winner = 'O';
} // 没有人获胜
if (winner == '\0') {
return false;
}
// 有人获胜,并提示是哪一个获胜
else {
// display winner
TextView T = ( TextView ) findViewById ( R.id.titleText );
T.setText ( winner + " wins" );
return true;
}
} /**
* 检查是哪一方获胜
*/
private boolean checkWinner ( char [ ][ ] board , int size ,
char player ) { // 检查每一行
for ( int x = 0 ; x < size ; x ++ ) {
int total = 0;
for ( int y = 0 ; y < size ; y ++ ) {
if (board [ x ] [ y ] == player) {
total ++ ;
}
}
if (total >= size) {
return true;
}
} // 检查每一列
for ( int y = 0 ; y < size ; y ++ ) {
int total = 0;
for ( int x = 0 ; x < size ; x ++ ) {
if (board [ x ] [ y ] == player) {
total ++ ;
}
}
if (total >= size) {
return true;
}
} // 检查X == Y的那条对角线
int total = 0;
for ( int x = 0 ; x < size ; x ++ ) {
for ( int y = 0 ; y < size ; y ++ ) {
if (x == y
&& board [ x ] [ y ] == player) {
total ++ ;
}
}
}
if (total >= size) {
return true;
} // 检查X != Y的对角线
total = 0;
for ( int x = 0 ; x < size ; x ++ ) {
for ( int y = 0 ; y < size ; y ++ ) {
if (x + y == size - 1
&& board [ x ] [ y ] == player) {
total ++ ;
}
}
} // 有人获胜
if (total >= size) {
return true;
} // 没有人获胜
return false;
} /**
* 有人获胜的情况下,禁用网格上的所有按钮
*/
private void disableButtons ( ) { TableLayout T = ( TableLayout ) findViewById ( R.id.tableLayout );
for ( int y = 0 ; y < T.getChildCount ( ) ; y ++ ) {
if (T.getChildAt ( y ) instanceof TableRow) {
TableRow R = ( TableRow ) T.getChildAt ( y );
for ( int x = 0 ; x < R.getChildCount ( ) ; x ++ ) {
if (R.getChildAt ( x ) instanceof Button) {
Button B = ( Button ) R.getChildAt ( x );
B.setEnabled ( false );
}
}
}
}
} /**
* 每次点击按钮时候的处理事件,得到点击的x和y坐标,
*/
private void setupOnClickListeners ( ) { TableLayout T = ( TableLayout ) findViewById ( R.id.tableLayout );
for ( int y = 0 ; y < T.getChildCount ( ) ; y ++ ) {
if (T.getChildAt ( y ) instanceof TableRow) {
TableRow R = ( TableRow ) T.getChildAt ( y );
for ( int x = 0 ; x < R.getChildCount ( ) ; x ++ ) {
View V = R.getChildAt ( x );
V.setOnClickListener ( new PlayOnClick (
x ,
y ) );
}
}
}
} /**
* Button 按钮的自定义点击事件,然后根据noughtsTurn是true还是false
* 来决定存储的是X还是O,并设置点击过的按钮为不可编辑
*
* 并检查是否有人获胜 ,然后把noughtsTurn取反 表示另外一个人在操作
*/
private class PlayOnClick implements View.OnClickListener { // 点击按钮的x坐标
private int x = 0; // 点击按钮的Y坐标
private int y = 0; public PlayOnClick (
int x , int y ) { this.x = x;
this.y = y;
} @ Override
public void onClick ( View view ) { if (view instanceof Button) {
Button B = ( Button ) view;
board [ x ] [ y ] = noughtsTurn ? 'O'
: 'X';
B.setText ( noughtsTurn ? "O"
: "X" );
B.setEnabled ( false );
noughtsTurn = ! noughtsTurn; // 检查是否有人获胜
if (checkWin ( )) {
disableButtons ( );
}
}
}
}
}
String.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Noughts & Crosses</string>
<string name="title">Naughts & Crosses - By Lyndon Armitage</string>
</resources>
Android --#字XO小游戏的更多相关文章
- 用Kotlin破解Android版微信小游戏-跳一跳
前言 微信又更新了,从更新日志上来看,似乎只是一次不痛不痒的小更新.不过,很快就有人发现,原来微信这次搞了个大动作——在小程序里加入了小游戏.今天也是朋友圈被刷爆的缘故. 看到网上 有人弄了一个破解版 ...
- 井字棋小游戏(C语言)
最近沉迷于<NetHack>.<DCSS>等字符游戏,对其很感兴趣,于是用C语言写了个字符界面的井字棋小游戏.欢迎大家指教. 编写时遇到了一些问题,我原先准备用循环,直到读取到 ...
- Android 经典欧美小游戏 guess who
本来是要做iOS开发的,因为一些世事无常和机缘巧合与测试工作还有安卓系统结下了不解之缘,前不久找到了guess who 源码,又加入了一些自己的元素最终完成了这个简单的小游戏. <?xml ve ...
- android Splashy Flash小游戏
今年刚開始学习android,自己在寝室捎带学习了下做APP,因为是刚開始学习,这个游戏仅仅注重了游戏的实现,实现了大概功能后.也没有实现游戏的细节,像分数图片的显示等.也没有注意代码的结构.一个字就 ...
- 使用Vue编写点击数字小游戏
使用vue编写一个点击数字计时小游戏,列入你在文本框中输入3,点击开始会生成一个3行3列的表格,表格数据为1-9随机排列,这时候从1开始点击,按顺序点到9,当按正确顺序点击完毕,会提示所用的时间,如果 ...
- 软件工程 Android小游戏 猜拳大战
一.前言 最近学校举办的大学生程序设计竞赛,自己利用课余时间写了一个小游戏,最近一直在忙这个写这个小游戏,参加比赛,最终是老师说自己写的简单,可以做的更复杂的点的.加油 二.内容简介 自己玩过Andr ...
- 介绍一款Android小游戏--交互式人机对战五子棋
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6589025 学习Android系统开发之余,编 ...
- [HTML5实现人工智能]小游戏《井字棋》发布,据说IQ上200才能赢
一,什么是TicTacToe(井字棋) 本 游戏 为在下用lufylegend开发的第二款小游戏.此游戏是大家想必大家小时候都玩过,因为玩它很简单,只需要一张草稿纸和一只笔就能开始游戏,所以广受儿 ...
- Android使用学习之画图(Canvas,Paint)与手势感应及其应用(乒乓球小游戏)
作为一个没有学习Android的菜鸟,近期一直在工作之外努力地学习的Android的使用. 这周看了下Android的画图.主要是Canvas,Paint等,感觉须要实践下.下午正好有空,就想整一个乒 ...
随机推荐
- foreach与Iterable学习
以前对于foreach的使用都是自然而然的感觉,没有深究过为什么可以用,什么时候可以用.最近才发现,原来那些可以使用的类,都是实现了Iterable接口的,否则根本就不能用. 下面是我之前学习时候写的 ...
- 攻城狮在路上(叁)Linux(二十一)--- linux磁盘检查 fsck \ badblocks
若系统掉电或磁盘发生问题,可利用fsck命令对文件系统进行检查.这一步是可选的,尽量少用. 使用前的建议:使用fsck命令时,被检查的分区务必不要挂载在系统上. 一.fsck: 命令格式:fsck [ ...
- hdu 5288 数学 ****
给一个序列 定义函数f(l ,r) 为区间[l ,r] 中 的数ai不是在这个区间其他任意数aj的倍数 求所有f(l,r)之和 通过预处理,记录 a[i] 的左右边界(所谓的左右边界时 在从 a[i] ...
- hdu 2232 矩阵 ***
一天四个不同的机器人a.b.c和d在一张跳舞毯上跳舞,这是一张特殊的跳舞毯,他由4个正方形毯子组成一个大的正方形毯子,一开始四个机器人分别站在4 块毯子上,舞蹈的每一步机器人可以往临近(两个毯子拥有同 ...
- HDU 3364 Lanterns 高斯消元
Lanterns Problem Description Alice has received a beautiful present from Bob. The present contains ...
- pythonchallenge之C++学习篇-00
前言 最近学习下C++,之前是python的用户,python解释器有诸多实现,其中最出名的要数C实现了,而且很多python的扩展模块可能要用C或者C++来写的,所以很有必要学习下C++了 为了避免 ...
- hash 分区的用途是什么?
Hash partitioning enables easy partitioning of data that does not lend itself to rangeor list partit ...
- 【SQL 数据库】将一张数据表信息复制到另一张数据表
一.MySQL数据库 1.如果目标表存在 INSERT INTO 目标表 SELECT * FROM 源表; 2.如果目标表不存在 CREATE TABLE 目标表 SELECT * FROM ...
- JMeter参数化(一)
JMeter参数化的4种方法:
- loadrunner中变量和参数之间的转化实例
1.变量转换成参数值的两种方法: 方法一: char *test="Agoly"; lr_save_string(test,"testPa"); lr_e ...