Penny Pitch Game

Arman Majumder Arman Majumder is offline Offline Jun 15th, 2007, 7:15 pm |
0
A JOptionPane version of the game of Penny Pitch. Uses numerical values spread on a board, and computer picks randomly chosen spot to place a penny and at the end of a run adds up the total score.

*Two separate programs, PennyPitchGame and Square (Square Class)*
Quick reply to this message  
Java Syntax
  1. // Arman Majumder
  2. // Penny Pitch Game
  3. // Requires Square Class <Square.java> Included in bottom
  4.  
  5. import javax.swing.*;
  6. import java.awt.*;
  7. import java.awt.event.*;
  8. import java.util.*;
  9. import java.io.*;
  10. import java.util.Random;
  11.  
  12. public class PennyPitchGame
  13. {
  14. public static void main(String[] args)
  15. {
  16.  
  17. Random gen = new Random();
  18. int totalScore = 0;
  19. int played = 0;
  20.  
  21. while(true)
  22. {
  23. Square[][] PennyBoard=
  24. {{new Square(1), new Square(1), new Square(1), new Square(1), new Square(1)},
  25. {new Square(1), new Square(2), new Square(2), new Square(2), new Square(1)},
  26. {new Square(1), new Square(2), new Square(3), new Square(2), new Square(1)},
  27. {new Square(1), new Square(2), new Square(2), new Square(2), new Square(1)},
  28. {new Square(1), new Square(1), new Square(1), new Square(1), new Square(1)}};
  29.  
  30. int round = 0;
  31. int row = 0;
  32. int col = 0;
  33. int attempt = 0;
  34. String str = "";
  35. int score = 0;
  36. boolean playAgain = true;
  37. int boardTotal = 0;
  38.  
  39. while(playAgain && round < 5)
  40. {
  41. int random = gen.nextInt(25) + 1;
  42. attempt = gen.nextInt(25) + 1;
  43. row = (attempt - 1) / 5;
  44. col = (attempt - 1) % 5;
  45. str = "";
  46.  
  47. while(!PennyBoard[row][col].pennyLanded())
  48. {
  49. if(playAgain && round == 0)
  50. {
  51. for(int row2 = 0; row2 < PennyBoard.length; row2++)
  52.  
  53. {
  54. for(int col2 = 0; col2 < PennyBoard.length; col2++)
  55.  
  56. {
  57. str += PennyBoard[row2][col2] + " ";
  58. }
  59. str += "\n";
  60. }
  61. JOptionPane.showMessageDialog(null,"The Original Board: \n " + str);
  62. str = "";
  63. }
  64.  
  65. score += PennyBoard[row][col].getNumber();
  66. PennyBoard[row][col].setPennyLanded();
  67. round++;
  68. boardTotal++;
  69.  
  70.  
  71. for(int row2 = 0; row2 < PennyBoard.length; row2++)
  72. {
  73. for(int col2 = 0; col2 < PennyBoard.length; col2++)
  74. {
  75. str += PennyBoard[row2][col2] + " ";
  76. }
  77. str += "\n";
  78. }
  79. JOptionPane.showMessageDialog(null, "The Board: \t" + boardTotal + " out of 5 \n" + str);
  80. }
  81. }
  82.  
  83. totalScore += score;
  84. played++;
  85. JOptionPane.showMessageDialog(null,"Score for this round is " + score + " points");
  86. JOptionPane.showMessageDialog(null,"Total is " + totalScore + " points");
  87.  
  88. int option = JOptionPane.showConfirmDialog(null, "¿Quitar?", "Select Option", JOptionPane.YES_NO_OPTION);
  89. if (option == 0)
  90. break;
  91. }
  92.  
  93. JOptionPane.showMessageDialog(null, "Total Status: " + totalScore + " points, in " + played + " rounds.");
  94. }
  95. }
  96.  
  97.  
  98. //////////////////////////////////////////////////////////////////////////////////
  99. //Square Class
  100. //New Program
  101. /////////////////////////////////////////////////////////////////////////////////
  102.  
  103. //Arman Majumder
  104. // Corresponds with Penny Pitch Game <PennyPitchGame.java>
  105. // Represents a square
  106.  
  107. public class Square extends Object
  108. {
  109. private int number;
  110. private boolean pennyLanded;
  111.  
  112. public Square(int n)
  113. {
  114. number = n;
  115. pennyLanded = false;
  116. }
  117.  
  118. public boolean pennyLanded()
  119. {
  120. return pennyLanded;
  121. }
  122.  
  123. public void setPennyLanded()
  124. {
  125. pennyLanded = true;
  126. }
  127.  
  128. public int getNumber()
  129. {
  130. return number;
  131. }
  132.  
  133. public String toString()
  134. {
  135. if (pennyLanded)
  136. return "P";
  137. else
  138. return "" + number;
  139. }
  140. }
0
nikhil_kpm82 nikhil_kpm82 is offline Offline | Jan 16th, 2008
this is so amazing work yaar !!!!!!!

can u send me some source codes of java games?
my e mail id is nikhil_kpm83@rediffmail.com
 
0
NovaWarlock NovaWarlock is offline Offline | Jun 5th, 2009
When I try to run it, it comes up with an error on line 107. For people that may retreive this error, remove "public" so it is just " class Square extends Object" and it should work.
 
 

Message:


Similar Threads
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC