当玩家赢得或输掉掷骰子游戏时重置pointValue

时间:2015-10-28 02:51:39

标签: java android eclipse

我试图在玩家获胜或失败时重置玩家的积分值,但我无法找出使用当前代码的最佳接近方法。我曾尝试在$dateTimeObject = \DateTime::createFromFormat('G:i', '9:30'); $dateTimeObject->format('H:i'); 时重置pointValue.setText("0"),但只是将字符串设置为" 0"并且round == 0永远不会重新初始化。我还尝试将pointwin布尔值设置为false,在won或lost if语句中设置losewin,然后在设置lose = true的{​​{1}}方法的结尾。我的尝试都没有奏效,我可能会过度思考。对于那些不知道怎么玩掷骰子的人:如果第一次投掷的总和是4,5,6,8,9或10,那么这个总和就成了玩家的“点”。要获胜,玩家必须继续滚动骰子直到点值滚动。在滚动点之前,玩家通过滚动7来输掉。

以下是我的MainActivity中使用上面提到的失败布尔尝试的代码

rollClicked()

我将代码全部包含在内,以便您可以看到我的声明和point = 0方法,但问题出在public class MainActivity extends Activity { // declarations private Button button1, button2; private ImageView dice1Image; private ImageView dice2Image; private TextView roll; private TextView point; private TextView results; private TextView betText; private TextView bankRollText; private String result; String betAmountString; String bankRollString; private Spinner bettingSpinner; int winNumber1 = 7; int winNumber2 = 11; int loseNumber1 = 2; int loseNumber2 = 3; int loseNumber3 = 12; int rounds = 0; int rollTotal = 0; int pointValue = 0; private int winsCounter = 0; private int loseCounter = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the view from activity_main.xml setContentView(R.layout.activity_main); // initialize objects dice1Image = (ImageView) findViewById(R.id.imageViewDie01); dice2Image = (ImageView) findViewById(R.id.imageViewDie02); roll = (TextView) findViewById(R.id.textViewRollTotal); point = (TextView) findViewById(R.id.textViewPoint); results = (TextView) findViewById(R.id.results); button1 = (Button) findViewById(R.id.howToPlayButton); button2 = (Button) findViewById(R.id.backToHomeButton); bettingSpinner = (Spinner) findViewById(R.id.betSpinner); betText = (TextView)findViewById(R.id.textViewBetAmount); bankRollText = (TextView)findViewById(R.id.textViewBuyInAmount); if (savedInstanceState == null) { results.setVisibility(View.GONE); } // get selected variable from spinner on HomeActivity TextView buyInText = (TextView)findViewById(R.id.textViewBuyInAmount); buyInText.setText(getIntent().getExtras().getString("buyInAmount")); // query the data, put it in the string-array and use ArrayAdapter constructor ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.bet, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_item); bettingSpinner.setAdapter(adapter); bettingSpinner.setOnItemSelectedListener(new function()); // restores instanceState if (savedInstanceState != null) { onRestoreInstanceState(savedInstanceState); } // Capture button clicks button1.setOnClickListener(new OnClickListener() { public void onClick(View arg1) { // Start NewActivity.class Intent myIntent = new Intent(MainActivity.this, NewActivity.class); //myIntent.putExtra("buyInAmount",buyInAmountGlobalVar); startActivity(myIntent); } }); // end setOnClickListener // Capture button clicks button2.setOnClickListener(new OnClickListener() { public void onClick(View arg2) { // Start HomeActivity.class Intent myIntent = new Intent(MainActivity.this, HomeActivity.class); startActivity(myIntent); } }); // end setOnClickListener } // end onCreate method int d1, d2; int betInt, bankRollInt; boolean win = false; boolean lose = false; // initializes rollClicked method when ROLL button is clicked public void rollClicked(View v) { int dice1, dice2; betInt = Integer.valueOf(bettingSpinner.getSelectedItem().toString()); bankRollInt = Integer.valueOf(bankRollText.getText().toString()); Random rand1 = new Random(); Random rand2 = new Random(); dice1 = rand1.nextInt(6) + 1; dice2 = rand2.nextInt(6) + 1; // sets values for dice images if(dice1 == 1) { dice1Image.setImageResource(R.drawable.die1); d1 = 1; } else if(dice1 == 2) { dice1Image.setImageResource(R.drawable.die2); d1 = 2; } else if(dice1 == 3) { dice1Image.setImageResource(R.drawable.die3); d1 = 3; } else if(dice1 == 4) { dice1Image.setImageResource(R.drawable.die4); d1 = 4; } else if(dice1 == 5) { dice1Image.setImageResource(R.drawable.die5); d1 = 5; } else if(dice1 == 6) { dice1Image.setImageResource(R.drawable.die6); d1 = 6; } if(dice2 == 1) { dice2Image.setImageResource(R.drawable.die1); d2 = 1; } else if(dice2 == 2) { dice2Image.setImageResource(R.drawable.die2); d2 = 2; } else if(dice2 == 3) { dice2Image.setImageResource(R.drawable.die3); d2 = 3; } else if(dice2 == 4) { dice2Image.setImageResource(R.drawable.die4); d2 = 4; } else if(dice2 == 5) { dice2Image.setImageResource(R.drawable.die5); d2 = 5; } else if(dice2 == 6) { dice2Image.setImageResource(R.drawable.die6); d2 = 6; } // calculate sum of dice for each roll if (rounds >= 0) { rollTotal = sum(dice1, dice2); roll.setText(String.valueOf(rollTotal)); } // win on first roll if((dice1 + dice2 == winNumber1 || (sum(dice1, dice2) == winNumber2)) && rounds == 0) { winsCounter++; bankRollInt = bankRollInt + betInt; bankRollText.setText(String.valueOf(bankRollInt)); results.setVisibility(View.VISIBLE); win = true; } // lose on first roll else if(rounds == 0 && (sum(dice1, dice2) == loseNumber1 || sum(dice1, dice2) == loseNumber2 || sum(dice1, dice2) == loseNumber3)){ loseCounter++; bankRollInt = bankRollInt - betInt; bankRollText.setText(String.valueOf(bankRollInt)); results.setVisibility(View.VISIBLE); results.setText("House wins! You Lose."); lose = true; } // display round results if no win or lose on first roll else if(rounds == 0) { pointValue = sum(dice1, dice2); results.setVisibility(View.VISIBLE); point.setText(String.valueOf(pointValue)); results.setText("Point established. It is " + pointValue + ". Roll Again."); } // lose if player hits 7 after first roll if(rounds > 0 && (sum(dice1, dice2) == 7)) { loseCounter++; bankRollInt = bankRollInt - betInt; bankRollText.setText(String.valueOf(bankRollInt)); results.setVisibility(View.VISIBLE); results.setText("House wins! You Lose."); lose = true; } // player wins if roll = pointValue after first roll else if(rounds > 0 && (sum(dice1, dice2) == pointValue)) { winsCounter++; bankRollInt = bankRollInt + betInt; bankRollText.setText(String.valueOf(bankRollInt)); results.setVisibility(View.VISIBLE); win = true; } // display round results if no win or lose after first roll else if(rounds > 0 && (sum(dice1, dice2) != pointValue && sum(dice1, dice2) != 7)){ results.setVisibility(View.VISIBLE); results.setText("No one wins. Keep rolling Dice."); } rounds++; // reset point if player wins or loses if (win = true){ point.setText("0"); } else if (lose = true){ point.setText("0"); } } // end rollClicked method // calculate sum of dice public int sum(int num1, int num2) { return num1 + num2; } // end sum method // clear board for new game public void resetGame(View v) { point.setText("0"); roll.setText("0"); results.setText(""); betText.setText("10"); bankRollText.setText("100"); // reset counter rounds = 0; } // end resetGame method @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } // end onOptionsItemSelected @Override public void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); // Save UI state changes to the savedInstanceState. // This bundle will be passed to onCreate if the process is // killed and restarted. result = results.getText().toString(); bankRollString = bankRollText.getText().toString(); betAmountString = betText.getText().toString(); savedInstanceState.putInt("POINT_VALUE", pointValue); savedInstanceState.putInt("ROLL_TOTAL", rollTotal); savedInstanceState.putString("RESULTS", result); savedInstanceState.putInt("DIE_1", d1); savedInstanceState.putInt("DIE_2", d2); savedInstanceState.putString("BANK_ROLL", bankRollString); savedInstanceState.putString("BET_AMOUNT", betAmountString); } // end onSavedInstanceState method @Override public void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); // Restore UI state from the savedInstanceState. // This bundle has also been passed to onCreate. pointValue = savedInstanceState.getInt("POINT_VALUE"); rollTotal = savedInstanceState.getInt("ROLL_TOTAL"); result = savedInstanceState.getString("RESULTS"); d1 = savedInstanceState.getInt("DIE_1"); d2 = savedInstanceState.getInt("DIE_2"); bankRollString = savedInstanceState.getString("BANK_ROLL"); betAmountString = savedInstanceState.getString("BET_AMOUNT"); roll.setText(String.valueOf(rollTotal)); point.setText(String.valueOf(pointValue)); results.setText(String.valueOf(result)); bankRollText.setText(String.valueOf(bankRollString)); betText.setText(String.valueOf(betAmountString)); // resets dice image values for onRestoreInstanceState if(d1 == 1) { dice1Image.setImageResource(R.drawable.die1); } else if(d1 == 2) { dice1Image.setImageResource(R.drawable.die2); } else if(d1 == 3) { dice1Image.setImageResource(R.drawable.die3); } else if(d1 == 4) { dice1Image.setImageResource(R.drawable.die4); } else if(d1 == 5) { dice1Image.setImageResource(R.drawable.die5); } else if(d1 == 6) { dice1Image.setImageResource(R.drawable.die6); } if(d2 == 1) { dice2Image.setImageResource(R.drawable.die1); } else if(d2 == 2) { dice2Image.setImageResource(R.drawable.die2); } else if(d2 == 3) { dice2Image.setImageResource(R.drawable.die3); } else if(d2 == 4) { dice2Image.setImageResource(R.drawable.die4); } else if(d2 == 5) { dice2Image.setImageResource(R.drawable.die5); } else if(d2 == 6) { dice2Image.setImageResource(R.drawable.die6); } } // end onRestoreInstanceState method public class function implements AdapterView.OnItemSelectedListener { public void onItemSelected(AdapterView<?> adapterView, View v, int pos, long l) { // passes selected spinner variable to textViewBetAmount String betString = adapterView.getItemAtPosition(pos).toString(); betText.setText(betString); } // end onItemSelected method public void onNothingSelected(AdapterView<?> adapterView) { } // end onNothingSelected method } // end function inner class } // end MainActivity class 方法中。任何指导都将不胜感激。

1 个答案:

答案 0 :(得分:0)

我想我有正确的想法创建一个布尔值,但我没有使用两个布尔值,而是简化了一些东西并创建了一个布尔值。我重置点的原始if语句没有重置该回合或包含rounds++计数器,我没有定义我的else语句(或者在这种情况下,如果{{1}会发生什么}或win = false)。

旧的重置点if语句:

lose = false

新的重置点if语句:

rounds++;

// reset point if player wins or loses
if (win = true){
    point.setText("0");
} else if (lose = true){
    point.setText("0");
}

以下是整个// reset point if player wins or loses else keep playing if(resetPointValue == true) { rounds = 0; pointValue = 0; } else { rounds++; } 方法:

rollClicked()

这绝对是一种过度思考和过度复杂化的案例。