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. MVC4 使用概述

    1.Bundle使用: http://www.cnblogs.com/inline/p/3897256.html 2.MVC总结: http://www.cnblogs.com/xlhblogs/ar ...

  2. sdut 2125串结构练习--字符串匹配【两种KMP算法】

    串结构练习——字符串匹配 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目链接:http://acm.sdut.edu.cn/sduto ...

  3. 机器学习系列:python

    工欲善其事,必先利其器!        机器学习的理论需要有编程语言才能得以实现,我选择 python 作为编程语言,网络上有篇不错的教程:python 初级教程:入门详解. 转载自http://ww ...

  4. UVA 10828 Back to Kernighan-Ritchie(高斯消元)

    高斯消元求概率 对于非起点,期望x[i] = ∑x[j] / deg[j] #include<cstdio> #include<iostream> #include<cs ...

  5. C# SMTP邮件发送 分类: C# 2014-07-13 19:10 333人阅读 评论(1) 收藏

    邮件发送在网站应用程序中经常会用到,包括您现在看到的博客,在添加评论后,系统会自动发送邮件通知到我邮箱的,把系统发送邮件的功能整理了下,做了一个客户端Demo,希望对有需要的童鞋有所帮助: 核心代码: ...

  6. Linux系统启动流程及安装命令行版本

    Debian安装 之前也安装过很多次linux不同版本的系统,但安装后都是直接带有桌面开发环境的版本,直接可以使用,正好最近项目不是很忙,想一直了解下Linux的整个启动流程,以及如何从命令行模式系统 ...

  7. VS2010和matlab2010混合编程中char16_t重定义的问题

    原因是VS2010中的yvals.h添加了char16_t的定义,而Matlab的matrix.h也包含对char16_t的定义,所以同时包含这两个头文件的话,会导致重复定义char16_t的错误.只 ...

  8. 10 个学习iOS开发的最佳网站(转)

    10 个学习iOS开发的最佳网站 作者 jopen 2012-09-26 08:59:56 1) Apple Learning Objective C Objective-C,通常写作ObjC和较少用 ...

  9. Sp EF输出 临时表

    -- ============================================= -- Author: <Author,,Name> -- Create date: < ...

  10. python web编程 创建一个web服务器

    这里就介绍几个底层的用于创建web服务器的模块,其中最为主要的就是BaseHTTPServer,很多框架和web服务器就是在他们的基础上创建的 基础知识 要建立一个Web 服务,一个基本的服务器和一个 ...