Homework 1MVC |
Modified: |
Download
Download then in Eclipse
File | Import | General | Existing Projects into Workspace | Open the download folder | Finish
Calculatorv2 Android Model-View-Controller example.
Overview
The purpose of the assignment is to gain Android development experience and Model-View-Controller programming patterns. The assignment is to implement a TicTacToe game in which the two players use the same Android device to make moves.
The game allows only valid moves (no changing the opponents X to O), and checks for wins or ties (when the board is filled). The game does not reset other than by starting execution in the debugger.
Here is a video of play.
Eclipse/Android
LF111, LF111A and LF115 computers should have Eclipse and Android SDK installed.
IUS
- Copy C:\temp\.android C:\Users\username\.android
- Start Eclipse by opening
C:\eclipse-jee-indigo-win32\eclipse
- Set the Android SDK location by:
Window | Preferences | Android | SDK Location: C:\Progra~1\Android\android-sdk
- File | New | Project | Android
ASSIGNMENT
Implement an Android TicTacToe game using Model-View-Controller pattern and MyModel.java below or similar model.
Suggested start:
- download the Calculator Android Model-View-Controller example,
- in Eclipse: File | Import | General | Existing Projects into Workspace and locate the uncompressed Calculatorv2 directory,
- debug Calculatorv2 project just to be sure it works,
- create a new project named TicTacToe,
- create TicTacToe files and paste from Calculator files:
- main.xml (no changes)
- display.xml (no changes)
- keyboard.xml (define buttons 0-8)
- MyController.java (implement TicTacToe control logic, including button event handlers).
- MyView.java (no changes)
- TicTacToeActivity.java from CalculatorActivity.java (minor changes)
- MyModel.java from below.
MyModel.java public class MyModel { private char board[]={'0','1','2','3','4','5','6','7','8'}; private char player='X'; private move='9'; public MyModel() { } public boolean setMove(char c) { // Return true on valid move int at = c-'0'; move = c; if(at < 0 || at > 8 || board[at] == 'X' || board[at] == 'O') return false; board [at] = player; if(player == 'X') player = 'O'; else player = 'X'; return true; } public boolean getWin() { // Return win state return (board[0]==board[3] && board[3]==board[6]) || (board[1]==board[4] && board[4]==board[7]) || (board[2]==board[5] && board[5]==board[8]) || (board[0]==board[1] && board[1]==board[2]) || (board[3]==board[4] && board[4]==board[5]) || (board[6]==board[7] && board[7]==board[8]) || (board[0]==board[4] && board[4]==board[8]) || (board[2]==board[4] && board[4]==board[6]); } public char getMove() { return move; }public char [] getBoard() { return board; } public char getPlayer() { return player; } public boolean getGameOver() { if(getWin()) return true; for(int i=0; i<9; i++) if(board[i] != 'X' && board[i] != 'O') return false; return true; } }
TURN IN
- compress the complete project folder
- upload to OnCourse as HW1
HINTS
button1.setText("X");
writes to LogCat which can be viewed in Eclipse by:
Window | Show View | LogCat
Clicking I filters to display only messages labeled with an I.