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小游戏的更多相关文章

  1. 用Kotlin破解Android版微信小游戏-跳一跳

    前言 微信又更新了,从更新日志上来看,似乎只是一次不痛不痒的小更新.不过,很快就有人发现,原来微信这次搞了个大动作——在小程序里加入了小游戏.今天也是朋友圈被刷爆的缘故. 看到网上 有人弄了一个破解版 ...

  2. 井字棋小游戏(C语言)

    最近沉迷于<NetHack>.<DCSS>等字符游戏,对其很感兴趣,于是用C语言写了个字符界面的井字棋小游戏.欢迎大家指教. 编写时遇到了一些问题,我原先准备用循环,直到读取到 ...

  3. Android 经典欧美小游戏 guess who

    本来是要做iOS开发的,因为一些世事无常和机缘巧合与测试工作还有安卓系统结下了不解之缘,前不久找到了guess who 源码,又加入了一些自己的元素最终完成了这个简单的小游戏. <?xml ve ...

  4. android Splashy Flash小游戏

    今年刚開始学习android,自己在寝室捎带学习了下做APP,因为是刚開始学习,这个游戏仅仅注重了游戏的实现,实现了大概功能后.也没有实现游戏的细节,像分数图片的显示等.也没有注意代码的结构.一个字就 ...

  5. 使用Vue编写点击数字小游戏

    使用vue编写一个点击数字计时小游戏,列入你在文本框中输入3,点击开始会生成一个3行3列的表格,表格数据为1-9随机排列,这时候从1开始点击,按顺序点到9,当按正确顺序点击完毕,会提示所用的时间,如果 ...

  6. 软件工程 Android小游戏 猜拳大战

    一.前言 最近学校举办的大学生程序设计竞赛,自己利用课余时间写了一个小游戏,最近一直在忙这个写这个小游戏,参加比赛,最终是老师说自己写的简单,可以做的更复杂的点的.加油 二.内容简介 自己玩过Andr ...

  7. 介绍一款Android小游戏--交互式人机对战五子棋

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6589025 学习Android系统开发之余,编 ...

  8. [HTML5实现人工智能]小游戏《井字棋》发布,据说IQ上200才能赢

    一,什么是TicTacToe(井字棋)   本 游戏 为在下用lufylegend开发的第二款小游戏.此游戏是大家想必大家小时候都玩过,因为玩它很简单,只需要一张草稿纸和一只笔就能开始游戏,所以广受儿 ...

  9. Android使用学习之画图(Canvas,Paint)与手势感应及其应用(乒乓球小游戏)

    作为一个没有学习Android的菜鸟,近期一直在工作之外努力地学习的Android的使用. 这周看了下Android的画图.主要是Canvas,Paint等,感觉须要实践下.下午正好有空,就想整一个乒 ...

随机推荐

  1. Filp Game

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25573   Accepted: 11052 题目链接: ...

  2. python中的Iterable, Iterator,生成器概念

    https://nychent.github.io/articles/2016-05/about-generator.cn 这个深刻 谈起Generator, 与之相关的的概念有 - {list, s ...

  3. java中常用的工具类(三)

    继续分享java中常用的一些工具类.前两篇的文章中有人评论使用Apache 的lang包和IO包,或者Google的Guava库.后续的我会加上的!谢谢支持IT江湖 一.连接数据库的综合类       ...

  4. java Integer和int的拆箱与装箱

    官网:http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html 1.赋值: a. 把int类型赋值给Integer类型:JVM会 ...

  5. zzy:java采用的是16位的Unicode字符集作为编码方式------理解

    java语言使用16位的Unicode字符集作为编码方式,是疯狂Java中的原话. 1,编码方式只是针对字符类型的(不包括字符串类,数值类型int等,这些只是在解释[执行]的时候放到Jvm的不同内存块 ...

  6. 编译器 expected unqualified-id before numeric constant 错误

    今天调试代码,碰到expected unqualified-id before numeric constant 错误,代码的错误模块出现在一个函数模块上, 奇怪的是这个函数模块之前编译了很多次,也没 ...

  7. Socket 通讯

    #import "ViewController.h" #import <sys/socket.h> #import <netinet/in.h> #impo ...

  8. 长按事件jquery mobile

    chat_enlarge.addEventListener('touchend', function(event) { if(fingers == 1){ event.preventDefault() ...

  9. (转载)Bash 中的特殊字符大全

    转自:https://linux.cn/article-5657-1.html Linux下无论如何都是要用到shell命令的,在Shell的实际使用中,有编程经验的很容易上手,但稍微有难度的是she ...

  10. AOP静态代理解析2-代码织入

    当我们完成了所有的AspectJ的准备工作后便可以进行织入分析了,首先还是从LoadTimeWeaverAwareProcessor开始. LoadTimeWeaverAwareProcessor实现 ...