This is question 2 in my assignment, we have to write it in php using eclipse.
Write a program to simulate two computer players rolling a die. The player which scores 100 (without exceeding 100) first wins.
During each turn the computer player has a choice: Play or Hold. This choice must be randomly decided by the program. If the choice is Play, the computer player rolls a die and the die value (e.g. 1 to 6) is added to the player’s score. Initial value of all scores should be 0 when the game starts. If the choice is Hold, the computer player skips its turn. The game will end when one of the two conditions occur. The first condition is when one of the computer player’s score exceeds 100. In this case, the other computer player will be indicated as the winner. The second condition is when both computer players’ score never exceed the 100 after 100 turns. In this case, it is considered a draw.
This is my code-Can someone tell me whats wrong please? it wouldnt stop at 100..
<?php
for($X==0;$X<100;$X++)
{
$rand1 = rand (1,2);//Random to see who starts first
If ($rand1 = 1)//Player 1 situation
{
$rand2 = rand(0,1);//Random to see Hold/play
If ($rand2 = 1)//Play scenario
{
$rand3 = rand(0,6);//random to see dice number
$score1 = $rand3 + $score1;
If ($score <= 100)
{
print "Player 1: Play<br>";
print "Dice rolled:$rand3<br>";
print "Current score $score1.<br>";
}
Else
{
print "Player 1 Lost!";
}
}
Else
{
print "Player 1: Hold<br>";
print "Current score:$scoreA<br>";
}
$rand2 = rand(0,1);//Random to see Hold/play
If ($rand2 = 1)//Play scenario
{
$rand4 = rand(0,6);//random to see dice number
$score2 = $rand4+$score2;
If ($score <= 100)
{
print "Player 2: Play<br>";
print "Dice rolled:$rand4<br>";
print "Current score $score2.<br>";
}
Else
{
print "Player 2 Lost!";
}
}
Else
{
print "Player 1: Hold<br>";
print "Current score:$scoreA<br>";
}
}
Else
{
$rand2 = rand(0,1);//Random to see Hold/play
If ($rand2 = 1)//Play scenario
{
$rand4 = rand(0,6);//random to see dice number
$score2 = $rand4+$score2;
If ($score <= 100)
{
print "Player 2: Play<br>";
print "Dice rolled:$rand4<br>";
print "Current score $score2.<br>";
}
Else
{
print "Player 2 Lost!";
}
}
Else
{
print "Player 1: Hold<br>";
print "Current score:$scoreA<br>";
}
$rand2 = rand(0,1);//Random to see Hold/play
If ($rand2 = 1)//Play scenario
{
$rand3 = rand(0,6);//random to see dice number
$score1 = $rand3+$score1;
If ($score <= 100)
{
print "Player 1: Play<br>";
print "Dice rolled:$rand3<br>";
print "Current score $score1.<br>";
}
Else
{
print "Player 1 Lost!";
}
}
Else
{
print "Player 1: Hold<br>";
print "Current score:$scoreA<br>";
}
}
}
?>
Someone, PLEASE help me!
Started by seraphican, Jul 11 2010 03:31 PM
4 replies to this topic
#1
Posted 11 July 2010 - 03:31 PM
|
|
|
#2
Posted 12 July 2010 - 09:10 AM
The reason why it wouldnt stop at 100 is because the for loop at the beginning depends on your X, which will loop for X until its value reaches 100; it does not take the score into account... The code you wrote is also very inefficient; for instance after the check for which players turn it is, it should run the same peice of code just with a variable to specify which player it is... You dont need to have the same peice twice for each player.
Oh and btw rand(0,6) is inclusive, which means a player can roll a 0?
Oh and btw rand(0,6) is inclusive, which means a player can roll a 0?
#3
Posted 13 July 2010 - 05:38 AM
Thank you so much, i have edited my codes and now its working. Especially the 0,6 part ;) so thanks again, really appreciate it :D cheers!
#4
Posted 13 July 2010 - 07:11 AM
Glad you got it working; post it up if you need any additional help. and also i forgot to mention use [code] tags around it so it makes it easier to read.
#5
Posted 20 July 2010 - 09:35 AM
Oh my god, this is the last question in my assignment and its really tearing my up >.< Do you have any idea how to do this? Would really appreciate it!
Write a program to simulate one computer player playing a simplified game of Minesweeper. The objective of the game is to find the hidden mines without detonating them. The computer player wins when it is able to identify the location of the mine without exploding them.
Assume there is no user input for this game. The computer player has a 4 x 4 2D array, which represents the land that may contain hidden mines. The program must randomly generate and hide a specific number of mines within the 4x4 2D land grid. Then, the program should simulate the game play as below:
• At each turn, the game board should be displayed
• At each turn, the computer player randomly selects a position on the land grid. Each position consists of a random x and y value (e.g. [2][2]).
• If the selected random position has no mine, a number is displayed. The number displayed can be as follows:
o 0 there are no mines in the right and left neighbours only
o 1 there is one mine in the right or left neighbour only
o 2 there are two mines in the right and left neighbours only
Then, the game awards 10 points to the player’s score.
• If the selected random position has a mine, then the program displays an appropriate message and the player loses 20 points from his current score.
• The program musn’t allow a selected random position to be re-selected again.
• At the end of the turn the updated 4x4 2D land grid should be displayed.
• Each position (or slot) in the displayed 4x4 2D land grid can contain any of the following:
o -1 not selected yet
o number selected and indicates the number of mines in the left & right neighbours only
o 99 mine found and exploded
• The game ends when one of the following conditions is met:
o The computer player selects all the positions except those having mines. If this condition is met the program should print the computer player as winner.
o The computer player’s score drops below 0. If this condition is satisfied the program should print the computer player as loser.
Your program must, at least, include the following methods/functions with the correct input parameters and output type:
i. initializeLandGrid, which will take in the player’s 2D array as input
and assign blank slots containing -1 (-1 represents unselected slot).
ii. assignMines, which will assign the land mines to randomly selected hidden locations in a 4x4 2D array. You may assume a total of 8 mines will be hidden.
Marks will be awarded for making your program modular further. You must carefully choose the methods.
Partial output of running game:
Turn 1
Land Grid
-1 -1 -1 -1
-1 -1 -1 -1
-1 -1 -1 -1
-1 -1 -1 -1
Position [1,1] selected
No mine found!
Score = 10
Turn 2
Land Grid
-1 -1 -1 -1
-1 1 -1 -1
-1 -1 -1 -1
-1 -1 -1 -1
Position [3,3] selected
Mind found!!! Too bad!
Score = 0
Turn 3
Land Grid
-1 -1 -1 -1
-1 1 -1 -1
-1 -1 -1 -1
-1 -1 -1 99
…
Write a program to simulate one computer player playing a simplified game of Minesweeper. The objective of the game is to find the hidden mines without detonating them. The computer player wins when it is able to identify the location of the mine without exploding them.
Assume there is no user input for this game. The computer player has a 4 x 4 2D array, which represents the land that may contain hidden mines. The program must randomly generate and hide a specific number of mines within the 4x4 2D land grid. Then, the program should simulate the game play as below:
• At each turn, the game board should be displayed
• At each turn, the computer player randomly selects a position on the land grid. Each position consists of a random x and y value (e.g. [2][2]).
• If the selected random position has no mine, a number is displayed. The number displayed can be as follows:
o 0 there are no mines in the right and left neighbours only
o 1 there is one mine in the right or left neighbour only
o 2 there are two mines in the right and left neighbours only
Then, the game awards 10 points to the player’s score.
• If the selected random position has a mine, then the program displays an appropriate message and the player loses 20 points from his current score.
• The program musn’t allow a selected random position to be re-selected again.
• At the end of the turn the updated 4x4 2D land grid should be displayed.
• Each position (or slot) in the displayed 4x4 2D land grid can contain any of the following:
o -1 not selected yet
o number selected and indicates the number of mines in the left & right neighbours only
o 99 mine found and exploded
• The game ends when one of the following conditions is met:
o The computer player selects all the positions except those having mines. If this condition is met the program should print the computer player as winner.
o The computer player’s score drops below 0. If this condition is satisfied the program should print the computer player as loser.
Your program must, at least, include the following methods/functions with the correct input parameters and output type:
i. initializeLandGrid, which will take in the player’s 2D array as input
and assign blank slots containing -1 (-1 represents unselected slot).
ii. assignMines, which will assign the land mines to randomly selected hidden locations in a 4x4 2D array. You may assume a total of 8 mines will be hidden.
Marks will be awarded for making your program modular further. You must carefully choose the methods.
Partial output of running game:
Turn 1
Land Grid
-1 -1 -1 -1
-1 -1 -1 -1
-1 -1 -1 -1
-1 -1 -1 -1
Position [1,1] selected
No mine found!
Score = 10
Turn 2
Land Grid
-1 -1 -1 -1
-1 1 -1 -1
-1 -1 -1 -1
-1 -1 -1 -1
Position [3,3] selected
Mind found!!! Too bad!
Score = 0
Turn 3
Land Grid
-1 -1 -1 -1
-1 1 -1 -1
-1 -1 -1 -1
-1 -1 -1 99
…


Sign In
Create Account

Back to top









