Penny Pitch Game

Arman Majumder 0 Tallied Votes 2K Views Share

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)*

// Arman Majumder  
// Penny Pitch Game
// Requires Square Class <Square.java>  Included in bottom

   import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;
   import java.util.*;
   import java.io.*;
   import java.util.Random;
   
    public class PennyPitchGame
   {
       public static void main(String[] args)
      {
         
         Random gen = new Random();
         int totalScore = 0;
         int played = 0;
         
         while(true)
         {
            Square[][] PennyBoard=
                 {{new Square(1), new Square(1), new Square(1), new Square(1), new Square(1)}, 
                  {new Square(1), new Square(2), new Square(2), new Square(2), new Square(1)},
                  {new Square(1), new Square(2), new Square(3), new Square(2), new Square(1)}, 
                  {new Square(1), new Square(2), new Square(2), new Square(2), new Square(1)},
                  {new Square(1), new Square(1), new Square(1), new Square(1), new Square(1)}};
           
            int round = 0;
            int row = 0;
            int col = 0;
            int attempt = 0;
            String str = "";
            int score = 0;
            boolean playAgain = true;
            int boardTotal = 0;
         
            while(playAgain && round < 5)
            {
               int random = gen.nextInt(25) + 1;
               attempt = gen.nextInt(25) + 1;
               row = (attempt - 1) / 5;
               col = (attempt - 1) % 5;
               str = "";
            
               while(!PennyBoard[row][col].pennyLanded())
               {
                  if(playAgain && round == 0)
                  {
                     for(int row2 = 0; row2 < PennyBoard.length; row2++)
                     
                     {
                        for(int col2 = 0; col2 < PennyBoard.length; col2++)   
                        
                        {
                           str += PennyBoard[row2][col2] + "   ";
                        }
                        str += "\n";
                     }
                     JOptionPane.showMessageDialog(null,"The Original Board: \n " + str);
                     str = "";
                  }
                  
                  score += PennyBoard[row][col].getNumber();
                  PennyBoard[row][col].setPennyLanded();
                  round++;
                  boardTotal++;
               
                  
                  for(int row2 = 0; row2 < PennyBoard.length; row2++)
                  {
                     for(int col2 = 0; col2 < PennyBoard.length; col2++)   
                     {
                        str += PennyBoard[row2][col2] + "   ";
                     }
                     str += "\n";
                  }
                  JOptionPane.showMessageDialog(null, "The Board: \t" + boardTotal + " out of 5 \n" + str);
               }
            }
            
            totalScore += score;
            played++;
            JOptionPane.showMessageDialog(null,"Score for this round is " + score + " points");
            JOptionPane.showMessageDialog(null,"Total is " + totalScore + " points");
           
            int option = JOptionPane.showConfirmDialog(null, "¿Quitar?", "Select Option", JOptionPane.YES_NO_OPTION);
            if (option == 0)
               break;
         }
         
         JOptionPane.showMessageDialog(null, "Total Status: " + totalScore + " points, in " + played + " rounds.");
      }
   }


//////////////////////////////////////////////////////////////////////////////////
//Square Class
//New Program
/////////////////////////////////////////////////////////////////////////////////

//Arman Majumder
// Corresponds with Penny Pitch Game <PennyPitchGame.java>
// Represents a square

    public class Square extends Object
   {
      private int number;
      private boolean pennyLanded;
   
       public Square(int n)
      {
         number = n;
         pennyLanded = false;
      }
   
       public boolean pennyLanded()
      {
         return pennyLanded;
      }
   
       public void setPennyLanded()
      {
         pennyLanded = true;
      }
   
       public int getNumber()
      {
         return number;
      }
   
       public String toString()
      {
         if (pennyLanded)
            return "P";
         else
            return "" + number;
      }
   }
nikhil_kpm82 0 Newbie Poster

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

NovaWarlock 0 Newbie Poster

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.