Skip to content

Commit

Permalink
Working pieces created
Browse files Browse the repository at this point in the history
  • Loading branch information
jaytaph committed Feb 22, 2011
1 parent a7deb2b commit 4e824be
Show file tree
Hide file tree
Showing 22 changed files with 578 additions and 216 deletions.
Binary file removed bin/PuzzleChess.apk
Binary file not shown.
Binary file removed bin/classes.dex
Binary file not shown.
2 changes: 1 addition & 1 deletion src/com/noxlogic/games/puzzlechess/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public Game getGame() {
* @param tag
* @return
*/
Piece findPiece(String tag) {
public Piece findPiece(String tag) {
for (Piece p : _pieces) {
if (p.getTag() == tag) return p;
}
Expand Down
9 changes: 1 addition & 8 deletions src/com/noxlogic/games/puzzlechess/ChessPanelView.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package com.noxlogic.games.puzzlechess;

import java.util.Random;

import com.noxlogic.games.puzzlechess.games.Game;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.graphics.Bitmap;
Expand Down Expand Up @@ -55,10 +51,7 @@ public void update() {
}
}

public void onDraw(Canvas canvas) {
Random r = new Random();
Log.d ("knights", "onDraw() called "+r.nextInt());

public void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);

// Draw background
Expand Down
4 changes: 1 addition & 3 deletions src/com/noxlogic/games/puzzlechess/ChessThread.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.noxlogic.games.puzzlechess;

import android.os.Message;
import android.util.Log;


class ChessThread extends Thread {
protected PuzzleChess _activity; // Need to know where to send messages to.
Expand All @@ -26,7 +24,7 @@ public ChessThread(PuzzleChess activity, int time, int fire_message) {
* @param state
*/
public void setRunning(boolean state) {
Log.d("knights", "SetRunning "+state);
// Log.d("knights", "SetRunning "+state);
_running = state;
}

Expand Down
58 changes: 44 additions & 14 deletions src/com/noxlogic/games/puzzlechess/Main.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package com.noxlogic.games.puzzlechess;


// @TODO: Implement game status & resets
// @TODO: Scalable boards + pieces
// @TODO: Add pieces

import java.util.ArrayList;

import com.noxlogic.games.puzzlechess.games.Game;
import com.noxlogic.games.puzzlechess.games.GameBishop1;
import com.noxlogic.games.puzzlechess.games.GameKnights1;
import com.noxlogic.games.puzzlechess.games.GameKnights2;
import com.noxlogic.games.puzzlechess.games.GameKnights3;
import com.noxlogic.games.puzzlechess.games.GameQueen1;
import com.noxlogic.games.puzzlechess.games.GameQueen2;
import com.noxlogic.games.puzzlechess.games.GameQueen3;
import com.noxlogic.games.puzzlechess.games.GameTest1;

import android.app.Activity;
import android.app.AlertDialog;
Expand Down Expand Up @@ -45,6 +49,15 @@ public void onCreate(Bundle icicle) {

// These are all the puzzles that are inside this game
GameRow gr;

gr = new GameRow();
gr.resource = R.drawable.wn;
gr.difficulty = 3;
gr.title = "TEST";
gr.subtitle = "TEST TEST";
gr.game = new GameTest1();
games.add(gr);

gr = new GameRow();
gr.resource = R.drawable.wn;
gr.difficulty = 3;
Expand All @@ -61,30 +74,47 @@ public void onCreate(Bundle icicle) {
gr.game = new GameKnights2();
games.add(gr);

gr = new GameRow();
gr.resource = R.drawable.wn;
gr.difficulty = 5;
gr.title = "Knights #3";
gr.subtitle = "Switch the knights around";
gr.game = new GameKnights3();
games.add(gr);

gr = new GameRow();
gr.resource = R.drawable.wq;
gr.difficulty = 2;
gr.title = "Kings mate";
gr.subtitle = "Checkmate the white king";
gr.difficulty = 1;
gr.title = "Queens #1";
gr.subtitle = "";
gr.game = new GameQueen1();
games.add(gr);

gr = new GameRow();
gr.resource = R.drawable.wq;
gr.difficulty = 2;
gr.title = "Something else";
gr.subtitle = "Switch the knights around";
gr.game = new GameKnights1();
gr.title = "Queens #2";
gr.subtitle = "";
gr.game = new GameQueen2();
games.add(gr);

gr = new GameRow();
gr.resource = R.drawable.wk;
gr.difficulty = 5;
gr.title = "Very difficult thingie";
gr.subtitle = "Switch the knights around";
gr.game = new GameKnights1();
gr.resource = R.drawable.wq;
gr.difficulty = 3;
gr.title = "Queens #3";
gr.subtitle = "";
gr.game = new GameQueen3();
games.add(gr);

gr = new GameRow();
gr.resource = R.drawable.wb;
gr.difficulty = 2;
gr.title = "Bishop #1";
gr.subtitle = "";
gr.game = new GameBishop1();
games.add(gr);


// Create
super.onCreate(icicle);
setContentView(R.layout.mainmenu);
Expand Down Expand Up @@ -139,7 +169,7 @@ public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo listrow = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();

// Get gamerow depending on the item selected
GameRow gr = games.get((int)listrow.id);
final GameRow gr = games.get((int)listrow.id);
the_id = (int)listrow.id;

switch (item.getItemId()) {
Expand All @@ -150,7 +180,7 @@ public boolean onContextItemSelected(MenuItem item) {
.setMessage("Are you sure you want to reset this game?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// @TODO: Add reset for game
gr.game.reset();
}

})
Expand Down
33 changes: 19 additions & 14 deletions src/com/noxlogic/games/puzzlechess/PuzzleChess.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
Expand All @@ -20,8 +19,8 @@ public class PuzzleChess extends Activity {
protected int _theme_id = 0;
protected boolean _trigger_onResume = false;

protected static final int MESSAGE_DURATION = 0x1000;
protected static final int MESSAGE_UPDATE = 0x1001;
protected static final int MESSAGE_DURATION = 0x1000; // Message fired every second (1s)
protected static final int MESSAGE_UPDATE = 0x1001; // Message fired when update is needed (10ms)

class timerThread implements Runnable{
public void run() {
Expand Down Expand Up @@ -52,7 +51,6 @@ public void onCreate(Bundle savedInstanceState) {
_panel = (ChessPanelView)findViewById(R.id.ChessPanel01);
_panel.setActivity(this);
_panel.setGame(app.getGame());


// Create chess thread for updates
_panel_thread = new ChessThread(this, 10, PuzzleChess.MESSAGE_UPDATE);
Expand All @@ -77,6 +75,7 @@ public void onClick(DialogInterface dialog, int id) {
})
.setMessage(app.getGame().getObjective())
.create();

goal.show();
}

Expand All @@ -88,28 +87,34 @@ public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_UPDATE :
_panel.update();

// Check state
boolean lost = _panel.getGame().hasLost();
boolean won = _panel.getGame().hasWon();
if (lost || won) {
// _panel_thread.stop();
// _timer_thread.stop();
// Stop timer and panel updates
_timer_thread.setRunning(false);
_panel_thread.setRunning(false);

// Display end results
String s = "";
if (won) s = "You have solved this game";
if (lost) s = "You have lost... :(";
if (won) s = "Congratulations. You have completed this game.";
if (lost) s = "Sorry. You've lost.";

new AlertDialog.Builder(PuzzleChess.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Game over")
.setTitle("Game ended")
.setMessage(s)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Reset game
_panel.getGame().reset();

// After the click, we return to main menu
finish();
}
})
.show();

}
break;
case MESSAGE_DURATION :
Expand Down Expand Up @@ -204,17 +209,17 @@ public void onClick(DialogInterface dialog, int item) {
}

public void onPause () {
Log.d("knights", "onPause()");
// Log.d("knights", "onPause()");
_timer_thread.setRunning(false);
_panel_thread.setRunning(false);

super.onPause();
}

public void onResume () {
Log.d("knights", "onResume()");
// Log.d("knights", "onResume()");
if (_trigger_onResume) {
Log.d("knights", "resuming threads");
// Log.d("knights", "resuming threads");
_timer_thread.setRunning(true);
_panel_thread.setRunning(true);
}
Expand Down
78 changes: 78 additions & 0 deletions src/com/noxlogic/games/puzzlechess/games/GameBishop1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.noxlogic.games.puzzlechess.games;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;

import com.noxlogic.games.puzzlechess.Board;
import com.noxlogic.games.puzzlechess.pieces.Bishop;
import com.noxlogic.games.puzzlechess.pieces.Piece;

public class GameBishop1 extends Game {

void init() {
setGameOptions(GAMEOPTION_UNLIMITEDMOVES);

boolean[][] fields = new boolean[8][8];
for (int y=0; y!=8; y++) {
for (int x=0; x!=8; x++) {
fields[x][y] = false;
}
}

for (int y=1; y!=6; y++) {
for (int x=2; x!=6; x++) {
fields[x][y] = true;
}
}

// Create new board
Board board = new Board(this, fields);

// Add pieces to the board
board.addPiece(new Bishop("wb1", Piece.WHITE), 2, 1);
board.addPiece(new Bishop("wb2", Piece.WHITE), 3, 1);
board.addPiece(new Bishop("wb3", Piece.WHITE), 4, 1);
board.addPiece(new Bishop("wb4", Piece.WHITE), 5, 1);

board.addPiece(new Bishop("bb1", Piece.BLACK), 2, 5);
board.addPiece(new Bishop("bb2", Piece.BLACK), 3, 5);
board.addPiece(new Bishop("bb3", Piece.BLACK), 4, 5);
board.addPiece(new Bishop("bb4", Piece.BLACK), 5, 5);

board.addDecorationToField(2, 1, plotDot(Color.BLACK));
board.addDecorationToField(3, 1, plotDot(Color.BLACK));
board.addDecorationToField(4, 1, plotDot(Color.BLACK));
board.addDecorationToField(5, 1, plotDot(Color.BLACK));

board.addDecorationToField(2, 5, plotDot(Color.WHITE));
board.addDecorationToField(3, 5, plotDot(Color.WHITE));
board.addDecorationToField(4, 5, plotDot(Color.WHITE));
board.addDecorationToField(5, 5, plotDot(Color.WHITE));

addBoard(board);
}

Bitmap plotDot(int color) {
Bitmap bmp = Bitmap.createBitmap(320, 320, Config.ARGB_8888);
Canvas bitmapcanvas = new Canvas(bmp);

Paint p = new Paint();
p.setColor(color);
bitmapcanvas.drawCircle(20, 20, 5, p);

return bmp;
}

public boolean hasWon() {
return false;
}

public String getObjective() {
// @TODO: FILL LATER
return "abc";
}

}
Loading

0 comments on commit 4e824be

Please sign in to comment.