penny pitch anyone?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • javaman90
    New Member
    • Feb 2007
    • 5

    penny pitch anyone?

    i am trying to make a penny pitch game. the sum is returned at the end of the game and the player has 2 options: toss and quit. the program does this when the toss option is clicked: generates two random numbers from teh row n column toss, add number at this position to the running total, and displays the board replacing the numbers with P's where pennies land.

    here is what the board looks like

    1 1 1 1 1 1 1
    1 2 2 2 2 2 1
    1 2 3 3 3 2 1
    1 2 3 5 3 2 1
    1 2 3 3 3 2 1
    1 2 2 2 2 2 1
    1 1 1 1 1 1 1

    the boards position are indexed starting at [1,1] in the top left, not [0,0], thus the rows n columns go from 1 to 7.

    i have succcessfully programed this in c++, but am unable to do so in java...arrays are just not my thing

    if anyone can assist, that would be great, thanks.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    What exactly do you need help with?

    Comment

    • javaman90
      New Member
      • Feb 2007
      • 5

      #3
      Originally posted by Ganon11
      What exactly do you need help with?
      i basically need help getting started, like setting up the array/game board...i can pretty much take care of the rest

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Well, that depends on what exactly needs to be in the game board. Will it be a 2D integer array? Will it be objects?

        The basic syntax will be:

        <Typename> board = new <Typename>[<xSize>][<ySize>];

        and then you can fill it like you would in C++.

        Comment

        • RedSon
          Recognized Expert Expert
          • Jan 2007
          • 4980

          #5
          This wouldn't perchance be a homework assignment would it? If it is, cool assignment, I never got to make a game in my classes.

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            In my classes, we weren't allowed to do 'games', as this would violate school policy. We worked on 'projects' with eerily familiar titles like Chess and Minesweeper.

            Comment

            • DeMan
              Top Contributor
              • Nov 2006
              • 1799

              #7
              Originally posted by Ganon11
              In my classes, we weren't allowed to do 'games', as this would violate school policy. We worked on 'projects' with eerily familiar titles like Chess and Minesweeper.
              Ah.... Touche'

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                I made it a point to make a game every two weeks.

                Comment

                • xballax23
                  New Member
                  • Mar 2007
                  • 1

                  #9
                  hello my wife is currenty in a comp sci class at the local university and if i dont do tthis for her i will be in some serious trouble!!!!!! please help me do the penny pitch program ASAP.....in 5 minutes and email with the source code would be preferable..... ........ <Removed:Agains t site rules>






                  Originally posted by javaman90
                  i am trying to make a penny pitch game. the sum is returned at the end of the game and the player has 2 options: toss and quit. the program does this when the toss option is clicked: generates two random numbers from teh row n column toss, add number at this position to the running total, and displays the board replacing the numbers with P's where pennies land.

                  here is what the board looks like

                  1 1 1 1 1 1 1
                  1 2 2 2 2 2 1
                  1 2 3 3 3 2 1
                  1 2 3 5 3 2 1
                  1 2 3 3 3 2 1
                  1 2 2 2 2 2 1
                  1 1 1 1 1 1 1

                  the boards position are indexed starting at [1,1] in the top left, not [0,0], thus the rows n columns go from 1 to 7.

                  i have succcessfully programed this in c++, but am unable to do so in java...arrays are just not my thing

                  if anyone can assist, that would be great, thanks.

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by xballax23
                    hello my wife is currenty in a comp sci class at the local university and if i dont do tthis for her i will be in some serious trouble!!!!!! please help me do the penny pitch program ASAP.....in 5 minutes and email with the source code would be preferable..... ........ <Removed:Agains t site rules>
                    We are not here to do students' assignments.
                    We only assist them to get it right.

                    Comment

                    • eaglesgoalscorer
                      New Member
                      • Apr 2007
                      • 1

                      #11
                      I have the same problem. Our class needs to make a penny pitch game with a board like follows: 1 1 1 1 1
                      1 2 2 2 1
                      1 2 3 2 1
                      1 2 2 2 1
                      1 1 1 1 1

                      I was wondering if some one could help me with the program not do it.

                      Directions:
                      a player tosses several pennies on the board aiming for the number with the highest value. At the end of the game, the total of the tosses is returned. Develop a program that plays this game. The program should perform the following steps each time the user selects the TOSS button: Generate 2 random numbers for the row and column toss. Add the number at this position to a running total. Display the board, replacing the numbers with P's where the pennies land.

                      Should use a 2D array of square objects. Each square contains a number like those above (board) and a boolean flag that indicates a penny has landed on a square.

                      Comment

                      • jazzed4jasper
                        New Member
                        • Jun 2009
                        • 4

                        #12
                        hey im doing this in my comp sci class i thought i had the code right but for some reason it wont work when i try to compile it. could someone please tell me whats wrong with my code? Id really appreciate it.

                        This is what I have:


                        [code=java]
                        import javax.swing.*;
                        import java.awt.*;
                        import java.awt.event. *;
                        import java.util.*;
                        import java.io.*;
                        import java.util.Rando m;

                        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(!PennyBoa rd[row][col].pennyLanded())
                        {
                        if(playAgain && round == 0)
                        {
                        for(int row2 = 0; row2 < PennyBoard.leng th; row2++)

                        {
                        for(int col2 = 0; col2 < PennyBoard.leng th; col2++)

                        {
                        str += PennyBoard[row2][col2] + " ";
                        }
                        str += "\n";
                        }
                        JOptionPane.sho wMessageDialog( null,"The Original Board: \n " + str);
                        str = "";
                        }

                        score += PennyBoard[row][col].getNumber();
                        PennyBoard[row][col].setPennyLanded ();
                        round++;
                        boardTotal++;


                        for(int row2 = 0; row2 < PennyBoard.leng th; row2++)
                        {
                        for(int col2 = 0; col2 < PennyBoard.leng th; col2++)
                        {
                        str += PennyBoard[row2][col2] + " ";
                        }
                        str += "\n";
                        }
                        JOptionPane.sho wMessageDialog( null, "The Board: \t" + boardTotal + " out of 5 \n" + str);
                        }
                        }

                        totalScore += score;
                        played++;
                        JOptionPane.sho wMessageDialog( null,"Score for this round is " + score + " points");
                        JOptionPane.sho wMessageDialog( null,"Total is " + totalScore + " points");

                        int option = JOptionPane.sho wConfirmDialog( null, "¿Quitar?", "Select Option", JOptionPane.YES _NO_OPTION);
                        if (option == 0)
                        break;
                        }

                        JOptionPane.sho wMessageDialog( null, "Total Status: " + totalScore + " points, in " + played + " rounds.");
                        }
                        }


                        //////////////////////////////////////////////////////////////////////////////////
                        //Square Class
                        //New Program
                        /////////////////////////////////////////////////////////////////////////////////
                        // 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;
                        }
                        }[/code]
                        Last edited by Nepomuk; Jun 4 '09, 11:53 AM. Reason: Please use [code] tags

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by jazzed4jasper
                          hey im doing this in my comp sci class i thought i had the code right but for some reason it wont work when i try to compile it. could someone please tell me whats wrong with my code? Id really appreciate it.
                          By "compile it" and "won't work" I assume the compiler complained about your code, i.e. it is syntactically incorrect. Compilers usually emit a readable error diagnostic message; one per error. Did you read them and understand them? If not why not copy/paste those messages here instead of making us guess.

                          kind regards,

                          Jos

                          Comment

                          • jazzed4jasper
                            New Member
                            • Jun 2009
                            • 4

                            #14
                            the error says:


                            class PennyPitchGame is public, should be declared in a file named PennyPitchGame. java
                            Last edited by jazzed4jasper; Jun 4 '09, 02:29 PM. Reason: spelling error

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #15
                              Originally posted by jazzed4jasper
                              the error says:


                              class PennyPitchGame is public, should be declared in a file named PennyPitchGame. java
                              And what is the name of the file?

                              kind regards,

                              Jos

                              Comment

                              Working...