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等,感觉须要实践下.下午正好有空,就想整一个乒 ...
随机推荐
- HTML5学习之文件操作(九)
之前我们操作本地文件都是使用flash.silverlight或者第三方的activeX插件等技术,由于使用了这些技术后就很进行跨平台的处理,另外就是让我们的web应用依赖了第三方的插件,而不是很独立 ...
- golang exec Command
package mainimport ( "fmt" "log" "os/exec")func main() { out, err := e ...
- PHPCMS-V9 获取一级栏目、二级栏目、三级栏目
<!-- 获取一级栏目 -->{pc:content action=" siteid="$siteid" order="listorder ASC& ...
- hdu 4044 2011北京赛区网络赛E 树形dp ****
专题训练 #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm ...
- [Tools] 使用XP远程登录Win8系统
[背景] 完成最基本的设置后,发现xp依然不能远程访问win8桌面,搜索后发现需要进一步设置 [开工] 按照参考资料进行设置,下面的参考资料已经写的很详细了,只是参考资料2中的文件名: redss ...
- 实时视频应用之QoS关键技术分析
转自:http://www.aiweibang.com/m/detail/104476372.html?from=p 随着WebRTC标准的逐步推广,实时音视频通讯技术受到越来越多公司和技术人员的关注 ...
- 阿里云(ECS)Centos服务器LNMP环境搭建
阿里云( ECS ) Centos7 服务器 LNMP 环境搭建 前言 第一次接触阿里云是大四的时候,当时在校外公司做兼职,关于智能家居项目的,话说当时俺就只有一个月左右的 php 后台开发经验(还是 ...
- Effective C++ 之 Item 1: 视C++为一个语言联邦
Effective C++ Chapter 1. 让自己习惯C++(Accustoming Yourself to C++) Item 1. 视C++为一个语言联邦(View C++ as a fed ...
- android:layout_gravity和android:gravity属性的区别(转)
gravity的中文意思就是”重心“,就是表示view横向和纵向的停靠位置 android:gravity:是对view控件本身来说的,是用来设置view本身的文本应该显示在view的什么位置,默认值 ...
- 接着上一篇 《Is WPF dead》
最近美国的PM传来消息,说微软在收集开发者的意见,会对WPF进行改进,微软会主要在1) performance 2) interop 3) touch and 4) access to WinRT A ...