Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.24 KB | None | 0 0
  1. import greenfoot.*;
  2. import java.util.*;
  3. import java.util.List;
  4. import java.util.Random;
  5. // IMPORTANT: If using older version of Greenfoot, uncomment this line.
  6. // If using newer version, comment out this line.
  7. //import java.awt.Color;
  8.  
  9. /**
  10. * Snakes in treasures game, in which the player gets point for digging up a pinapple, gets warned or killed if they find a blueberry or is granted immunity if they find a Costa Rican Steve Aronson
  11. *
  12. * @author Everett Miceli
  13. */
  14. public class Example extends World
  15. {
  16. private static final int COLUMNS = 5;
  17. private static final int ROWS = 5;
  18. private static final int CELL_SIZE = 100;
  19. private static final int SKIP_TURN=3;
  20. private static final int POWER_UP=0;
  21. private static final int PINEAPPLE=1;
  22. private static final int BLUEBERRY=2;
  23. private static int screen[][] = new int[ROWS][COLUMNS];
  24. private boolean player1Turn=true;
  25. private static int scores[] = new int[2];
  26. private static int wins[] = new int[]{0,0};
  27. private boolean gameOver=false;
  28. private boolean warning=false;
  29. private boolean end=false;
  30. private int difficulty=1;
  31. private int powerUpChance=10;
  32. private int pineappleChance=25;
  33. private int blueberryChance=55;
  34. private int powerUpLeft=0;
  35. public Example()
  36. {
  37. super(COLUMNS, ROWS, CELL_SIZE);
  38. setUp();
  39. }
  40.  
  41. /**
  42. * Sets up the board and resets everything to its original values
  43. */
  44. public void setUp()
  45. {
  46. player1Turn=true;
  47. Random rnd = new Random();
  48. difficulty=getDifficulty();
  49. checkDifficulty();
  50. for (int i =0;i<ROWS;i++)
  51. {
  52. for (int k =0;k<COLUMNS;k++)
  53. {
  54. int g = rnd.nextInt(100);
  55. if (g<powerUpChance)
  56. screen[i][k] = 0;
  57. else if (g>pineappleChance && g<blueberryChance)
  58. screen[i][k] = 1;
  59. else
  60. screen[i][k] = 2;
  61. }
  62. }
  63. screen[ROWS-1][COLUMNS-1] = SKIP_TURN;
  64. clearBoard();
  65. drawText("SKIP TURN", 4*CELL_SIZE + CELL_SIZE/2, 4*CELL_SIZE+CELL_SIZE/2);
  66. scores[0]=0;
  67. scores[1]=0;
  68. warning=false;
  69. gameOver=false;
  70. }
  71.  
  72. /**
  73. * Checks to see if the player's difficulty input is valid
  74. */
  75. public boolean isDiff(String str)
  76. {
  77. int[] difficulties = new int[]{1,2,3,4};
  78. for(int i:difficulties)
  79. {
  80. if (str.equals(i))
  81. return true;
  82. }
  83. return false;
  84. }
  85.  
  86. /**
  87. * Changes who's turn it is
  88. */
  89. public void changePlayer()
  90. {
  91. if (player1Turn){
  92. player1Turn=!player1Turn;
  93. warning=false;
  94. powerUpLeft=0;
  95. System.out.println("Player 2's turn");
  96. }
  97. else
  98. gameOver=true;
  99. }
  100.  
  101. /**
  102. * Gets the difficulty level from the player
  103. */
  104. public int getDifficulty()
  105. {
  106. String diff = Greenfoot.ask("Pick a number between 1-4");
  107. if (diff!= null && isDiff(diff))
  108. return Integer.parseInt(diff);
  109. return 1;
  110. }
  111.  
  112. /**
  113. * Clears the board of all pictures
  114. */
  115. public void clearBoard()
  116. {
  117. GreenfootImage pic = new GreenfootImage("white.jpg");
  118. for (int i =0;i<ROWS;i++)
  119. {
  120. for (int k =0;k<COLUMNS;k++)
  121. {
  122. getBackground().drawImage(pic, i*CELL_SIZE, k*CELL_SIZE);
  123. }
  124. }
  125. }
  126.  
  127. /**
  128. * Checks the current difficulty level and changes the chances of each item.
  129. */
  130. public void checkDifficulty()
  131. {
  132. if (difficulty==4)
  133. {
  134. pineappleChance = 40;
  135. blueberryChance=50;
  136. }
  137. else if (difficulty==3)
  138. {
  139. pineappleChance=35;
  140. blueberryChance=55;
  141. }
  142. else if (difficulty==2)
  143. {
  144. pineappleChance=30;
  145. blueberryChance=60;
  146. }
  147. }
  148.  
  149. /**
  150. * Gets the player's mouse position and flips over the card at that location. Mouse null position checker by Super_Hippo on greenfoot.org
  151. */
  152. public void act()
  153. {
  154. if (gameGoing()){
  155. MouseInfo x =Greenfoot.getMouseInfo();
  156. if (Greenfoot.mouseClicked(this) && x!=null) {
  157. System.out.println(gameOver);
  158. int row = Greenfoot.getMouseInfo().getY();
  159. int col = Greenfoot.getMouseInfo().getX();
  160. int loc = screen[row][col];
  161. if (loc==SKIP_TURN){
  162. System.out.println("Turn skipped");
  163. player1Turn=!player1Turn;
  164. }
  165. else if (loc == -1)
  166. {
  167. drawText("You already dug here", row*CELL_SIZE + CELL_SIZE/2, col*CELL_SIZE+CELL_SIZE/2);
  168. }
  169. else if (loc==POWER_UP){
  170. drawPic(row, col,"steve.png");
  171. powerUpLeft+=2;
  172. }
  173.  
  174. else if (loc==PINEAPPLE){
  175. drawPic(row, col,"pa.png");
  176. Greenfoot.playSound("success.wav");
  177. updateScore();
  178. }
  179. else {
  180. drawPic(row, col,"bb.png");
  181. if (powerUpLeft<1)
  182. updateWarnings();
  183. else
  184. powerUpLeft--;
  185. }
  186. if(loc!=3)
  187. screen[row][col] = -1;
  188. }
  189. }
  190. end();
  191. }
  192.  
  193. /**
  194. * Ends the game
  195. */
  196. public void end()
  197. {
  198. if (end){
  199. System.out.println("Game over");
  200. if(scores[0] > scores[1]){
  201. System.out.println("Player 1 wins");
  202. wins[0]++;
  203. }
  204. else{
  205. System.out.println("Player 2 wins");
  206. wins[1]++;
  207. }
  208. System.out.println("Player 1 has " + wins[0] + " wins." + " Player 2 has " + wins[1] + " wins.");
  209. end=false;
  210. setUp();
  211. }
  212. }
  213.  
  214. /**
  215. * Updates the warning system and kills the player if they already have one
  216. */
  217. public void updateWarnings()
  218. {
  219. if (warning && !player1Turn){
  220. gameOver=true;
  221. scores[1]=0;
  222. }
  223. else if (warning && player1Turn){
  224. player1Turn = !player1Turn;
  225. scores[0]=0;
  226. warning=false;
  227. }
  228. else
  229. warning=true;
  230. }
  231.  
  232. /**
  233. * Checks to see if the game is still going
  234. */
  235. public boolean gameGoing()
  236. {
  237. if (allFlipped() || gameOver){
  238. end=true;
  239. return false;
  240. }
  241. return true;
  242. }
  243.  
  244. /**
  245. * Checks to see if all of the cards have been flipped over
  246. */
  247. public boolean allFlipped()
  248. {
  249. for (int i =0;i<ROWS;i++)
  250. {
  251. for (int k =0;k<COLUMNS;k++)
  252. {
  253. if (screen[i][k]!=-1)
  254. return false;
  255. }
  256. }
  257. return true;
  258. }
  259.  
  260. /**
  261. * Skips the players turn or ends the game if it is player 2's turn
  262. */
  263. public void skipTurn()
  264. {
  265. System.out.println("Turn skipped");
  266. changePlayer();
  267. warning=false;
  268. }
  269.  
  270. /**
  271. * Updates the player's score
  272. */
  273. private void updateScore()
  274. {
  275. if(player1Turn)
  276. scores[0]++;
  277. else
  278. scores[1]++;
  279. System.out.println("p1 " + scores[0] + "p2 " + scores[1]);
  280. }
  281.  
  282. /**
  283. * Draw text in a given cell
  284. */
  285. public void drawText(String str, int row, int col)
  286. {
  287. GreenfootImage imageText = new GreenfootImage(str, 10, Color.BLACK, Color.RED);
  288. getBackground().drawImage(imageText, col, row);
  289.  
  290. }
  291.  
  292. /**
  293. * Draws a picture
  294. */
  295. public void drawPic(int row, int col, String name)
  296. {
  297. GreenfootImage pic = new GreenfootImage(name);
  298. pic.scale(CELL_SIZE,CELL_SIZE);
  299. getBackground().drawImage(pic, col*CELL_SIZE, row*CELL_SIZE);
  300. }
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement