Last active
April 8, 2017 11:06
-
-
Save aliasgarlabs/a0233caddaa99cabb229d7246daec2ea to your computer and use it in GitHub Desktop.
Scarne's Dice Source Code - Here you'll find all the resources required for Scarne's Dice Game for the Android Workshop at SIT on 8 April 2017.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
<TextView | |
android:id="@+id/tv_player_turn" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center" | |
android:layout_marginTop="20dp" | |
android:text="Player Turn: "/> | |
<ImageView | |
android:id="@+id/image" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center" | |
android:layout_marginTop="30dp" | |
android:src="@drawable/dice1"/> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal" | |
android:layout_marginTop="20dp"> | |
<TextView | |
android:id="@+id/p1_turn" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="P1 Turn Score: "/> | |
<TextView | |
android:id="@+id/p2_turn" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:gravity="right" | |
android:text="P2 Turn Score: "/> | |
</LinearLayout> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal" | |
android:layout_marginTop="20dp"> | |
<TextView | |
android:id="@+id/p1_total" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="P1 Total Score: "/> | |
<TextView | |
android:id="@+id/p2_total" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:gravity="right" | |
android:text="P2 Total Score: "/> | |
</LinearLayout> | |
<RelativeLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:gravity="center" | |
android:layout_marginTop="20dp"> | |
<Button | |
android:id="@+id/b_roll" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="ROLL"/> | |
<Button | |
android:id="@+id/b_hold" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="HOLD" | |
android:layout_toRightOf="@id/b_roll" | |
/> | |
<Button | |
android:id="@+id/b_reset" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="RESET" | |
android:layout_toRightOf="@id/b_hold"/> | |
</RelativeLayout> | |
</LinearLayout> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Download dice imageset from here: https://drive.google.com/drive/folders/0B6CdjgnUubyxXzZEVUJ3U0J1Y00?usp=sharing | |
Unzip the zip file and place all 6 images in app>src>main>res>drawable folder |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sit.dicegame; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import java.util.Random; | |
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ | |
int WINNING_VALUE = 50; | |
TextView playerTurn, p1Turn, p2Turn, p1Total, p2Total; | |
Button bRoll, bHold, bReset; | |
ImageView imageDice; | |
int p1TotalScore,p2TotalScore,diceValue; | |
int score = 0; | |
boolean isP1Turn = true; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
playerTurn = (TextView) findViewById(R.id.tv_player_turn); | |
p1Turn = (TextView) findViewById(R.id.p1_turn); | |
p2Turn = (TextView) findViewById(R.id.p2_turn); | |
p1Total = (TextView) findViewById(R.id.p1_total); | |
p2Total = (TextView) findViewById(R.id.p2_total); | |
bRoll = (Button) findViewById(R.id.b_roll); | |
bHold = (Button) findViewById(R.id.b_hold); | |
bReset = (Button) findViewById(R.id.b_reset); | |
imageDice = (ImageView) findViewById(R.id.imageView); | |
//TOUGH | |
bRoll.setOnClickListener(this); | |
bHold.setOnClickListener(this); | |
bReset.setOnClickListener(this); | |
} | |
public void roll() | |
{ | |
Random random = new Random(); | |
diceValue = random.nextInt(5)+1; | |
if(diceValue == 1){ | |
score = 0; | |
finalizeScore(); | |
} else { | |
score += diceValue; // score = score + diceValue; | |
} | |
updateViews(); | |
} | |
public void finalizeScore(){ | |
if(isP1Turn){ | |
p1TotalScore += score; | |
if(p1TotalScore>=WINNING_VALUE) | |
gameOver(); | |
} else { | |
p2TotalScore += score; | |
if(p2TotalScore>=WINNING_VALUE) | |
gameOver(); | |
} | |
isP1Turn = !isP1Turn; | |
score = 0; | |
} | |
private void gameOver() { | |
if(isP1Turn) | |
{ | |
playerTurn.setText("Player 1 wins!!!"); | |
Toast.makeText(this,"Player 1 wins!!!",Toast.LENGTH_SHORT).show(); | |
} | |
else | |
{ | |
playerTurn.setText("Player 2 wins!!!"); | |
Toast.makeText(this,"Player 2 wins!!!",Toast.LENGTH_SHORT).show(); | |
} | |
} | |
public void hold() | |
{ | |
finalizeScore(); | |
updateViews(); | |
} | |
public void reset() | |
{ | |
score = p1TotalScore = p2TotalScore = 0; | |
isP1Turn = true; | |
updateViews(); | |
} | |
private void updateViews() { | |
if(isP1Turn) | |
playerTurn.setText("Player 1 Turn"); | |
else | |
playerTurn.setText("Player 2 Turn"); | |
switch (diceValue) | |
{ | |
case 1: | |
imageDice.setImageDrawable(getResources().getDrawable(R.drawable.dice1)); | |
break; | |
case 2: | |
imageDice.setImageDrawable(getResources().getDrawable(R.drawable.dice2)); | |
break; | |
case 3: | |
imageDice.setImageDrawable(getResources().getDrawable(R.drawable.dice3)); | |
break; | |
case 4: | |
imageDice.setImageDrawable(getResources().getDrawable(R.drawable.dice4)); | |
break; | |
case 5: | |
imageDice.setImageDrawable(getResources().getDrawable(R.drawable.dice5)); | |
break; | |
case 6: | |
imageDice.setImageDrawable(getResources().getDrawable(R.drawable.dice6)); | |
break; | |
} | |
if(isP1Turn) | |
{ | |
p1Turn.setText("P1 Turn Score: " + score); | |
p2Turn.setText("P2 Turn Score: 0"); | |
} | |
else | |
{ | |
p2Turn.setText("P2 Turn Score: " + score); | |
p1Turn.setText("P1 Turn Score: 0"); | |
} | |
p1Total.setText("P1 Total Score: "+p1Total); | |
p2Total.setText("P2 Total Score: "+p2Total); | |
} | |
@Override | |
public void onClick(View v) { | |
switch (v.getId()) | |
{ | |
case R.id.b_roll: | |
roll(); | |
break; | |
case R.id.b_hold: | |
hold(); | |
break; | |
case R.id.b_reset: | |
reset(); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment