Skip to content

Commit

Permalink
Display timer and number of moves
Browse files Browse the repository at this point in the history
  • Loading branch information
remram44 committed Apr 21, 2016
1 parent 08023dd commit bef2de2
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 7 deletions.
6 changes: 4 additions & 2 deletions app/src/main/java/fr/remram/taquindroid/EndGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ public void onCreate(Bundle savedInstanceState)
else
image.setImageDrawable(getResources().getDrawable(R.drawable.image));

// TODO : Display game time
float time = intent.getLongExtra("time", 0) / 1000.0f;
int moves = intent.getIntExtra("moves", 0);

TextView t = (TextView) findViewById(R.id.endgame_label);
t.setText(getString(R.string.end_infos, width, height));
t.setText(getString(R.string.end_infos, width, height, time, moves));
}

}
18 changes: 17 additions & 1 deletion app/src/main/java/fr/remram/taquindroid/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class Game extends Activity implements GameView.EndGameListener {
private int m_Width;
private int m_Height;

private Stopwatch m_GameTime = new Stopwatch();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
Expand Down Expand Up @@ -57,13 +59,27 @@ public void onCreate(Bundle savedInstanceState)
}

@Override
public void onGameEnded()
protected void onPause() {
super.onPause();
m_GameTime.stop();
}

@Override
protected void onResume() {
super.onResume();
m_GameTime.start();
}

@Override
public void onGameEnded(int moves)
{
Intent end_game = new Intent();
end_game.setClass(this, EndGame.class);
end_game.setData(m_SelectedImage);
end_game.putExtra("width", m_Width);
end_game.putExtra("height", m_Height);
end_game.putExtra("time", m_GameTime.milliseconds());
end_game.putExtra("moves", moves);
startActivity(end_game);
finish();
}
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/fr/remram/taquindroid/GameView.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private class Block {

public interface EndGameListener {

void onGameEnded();
void onGameEnded(int moves);

}

Expand All @@ -41,6 +41,7 @@ public interface EndGameListener {
private int m_TileSizeY;
private float m_TileAspect;
private EndGameListener m_EndGameListener;
private int m_Moves = 0;

public GameView(Context context, Bitmap image, int width, int height)
{
Expand Down Expand Up @@ -168,6 +169,7 @@ else if(m_ActiveBlock != -1)
m_Grid[m_EmptyX][m_EmptyY] = -1;
m_Grid[m_Blocks[m_ActiveBlock].x][m_Blocks[m_ActiveBlock].y] = m_ActiveBlock;
m_ActiveBlock = -1;
m_Moves++;
checkEnd();
}

Expand Down Expand Up @@ -218,7 +220,7 @@ private void checkEnd()
if(m_Grid[x][y] != y*m_Width + x)
return ;
}
m_EndGameListener.onGameEnded();
m_EndGameListener.onGameEnded(m_Moves);
}

}
51 changes: 51 additions & 0 deletions app/src/main/java/fr/remram/taquindroid/Stopwatch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package fr.remram.taquindroid;

import android.os.SystemClock;
import android.util.Log;

public class Stopwatch {

private long m_StartTime = -1;
private long m_StopTime = -1;

public void start()
{
Log.v("TIMER", String.format("start(); %d %d", m_StartTime, m_StopTime));
if(m_StartTime == -1)
m_StartTime = SystemClock.elapsedRealtime();
else if(m_StopTime != -1)
{
long now = SystemClock.elapsedRealtime();
long stopped_for = now - m_StopTime;
m_StartTime += stopped_for;
m_StopTime = -1;
}
Log.v("TIMER", String.format(" %d %d", m_StartTime, m_StopTime));
}

public void stop()
{
Log.v("TIMER", String.format("stop(); %d %d", m_StartTime, m_StopTime));
if(m_StopTime == -1)
m_StopTime = SystemClock.elapsedRealtime();
Log.v("TIMER", String.format(" %d %d", m_StartTime, m_StopTime));
}

public boolean stopped()
{
return m_StopTime != -1;
}

public long milliseconds()
{
long now = SystemClock.elapsedRealtime();
long stopped_for = 0;
if(m_StopTime != -1)
stopped_for = now - m_StopTime;
Log.v("TIMER", String.format("? %d %d; %d - %d - %d = %d", m_StartTime, m_StopTime,
now, m_StartTime, stopped_for,
now - m_StartTime - stopped_for));
return now - m_StartTime - stopped_for;
}

}
2 changes: 1 addition & 1 deletion app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Taquindroid</string>
<string name="end_infos">Puzzle %1$dx%2$d résolu !</string>
<string name="end_infos">Puzzle %1$dx%2$d résolu !\nTemps : %3$1fs, movements : %4$d</string>
<string name="menu_play">Jouer</string>
<string name="menu_options">Options</string>
<string name="menu_scores">Scores</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Taquindroid</string>
<string name="end_infos">Puzzle %1$dx%2$d solved!</string>
<string name="end_infos">Puzzle %1$dx%2$d solved!\nTime: %3$1fs, moves: %4$d</string>
<string name="menu_play">Play</string>
<string name="menu_options">Options</string>
<string name="menu_scores">Scores</string>
Expand Down

0 comments on commit bef2de2

Please sign in to comment.