Homework 1

MVC

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

  1. Copy C:\temp\.android   C:\Users\username\.android
     
  2. Start Eclipse by opening

        C:\eclipse-jee-indigo-win32\eclipse
     
  3. Set the Android SDK location by:

        Window | Preferences | Android | SDK Location: C:\Progra~1\Android\android-sdk
     
  4. File | New | Project | Android

ASSIGNMENT

Implement an Android TicTacToe game using Model-View-Controller pattern and MyModel.java below or similar model.

Suggested start:

  1. download the Calculator Android Model-View-Controller example,
  2. in Eclipse: File | Import | General | Existing Projects into Workspace and locate the uncompressed Calculatorv2 directory,
  3. debug Calculatorv2 project just to be sure it works,
  4. create a new project named TicTacToe,
  5. create TicTacToe files and paste from Calculator files:
    1. main.xml (no changes)
    2. display.xml (no changes)
    3. keyboard.xml (define buttons 0-8)
    4. MyController.java (implement TicTacToe control logic, including button event handlers).
    5. MyView.java (no changes)
    6. TicTacToeActivity.java from CalculatorActivity.java (minor changes)
    7. 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

HINTS