Results 1 to 12 of 12
  1. #1
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Exclamation how to log into a game with my player.java class

    Hi people! i am new to java programming and i have to create a player.java class that has to do the following:

    store all information related to a player of the game (ex: Name, Id, Rounds played, Rounds Won) in private instance variables. It must also have methods to allow a player to:
    a) Login this will test a player's Id against a file (whose name is passed into the method as a parameter). If the id is found in the file the player's logged status will be marked as "true"; otherwise it will be marked as "false".

    b) Register this will test a player's Id against a file (whose name is passed into the method as a parameter). If the Id is not found in the file it will be added to the end of the file and the player's logged status will be marked as "true"; otherwise it will be marked as "false".

    c) Update his/her wins and update his/her losses (since the main program will not be able to access the instance variables directly)

    d) Return a formatted String version of the player's information for output

    e) Write the player's information back to the appropriate file (based on the player's Id).

    Here is my code
    Java Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class Player
    {
      
        
    
            //Instance Fields     
                private Scanner inScan;
                private String firstName, lastName, Id; 
                private int roundsPlayed, roundsWon;
           
                
             /** Constructor 
                 N The users full name 
                 i The id
                 Rp The number of rounds played
                 Rw The number of round player has won
              */
                
             
                public Player (String i, String f, String l, int rp, int rw, Scanner s){
                    
                    Id = new String(i);
                    firstName = new String(f);
                    lastName = new String(l);
                    inScan = s; 
                  } // end constructor 
                
                
                
                public String toString()
                {
                    StringBuffer S = new StringBuffer();
                    S.append("Id:" + Id + "   ");
                    S.append("LastName:" + lastName + "   ");
                    S.append("FirstName:" + firstName + "   ");
                     return S.toString();
               }
                
                
                
                
                
                
                public static String Login (String Id, String firstName, String lastName) // this method is for getting the login information 
                {
                    
                    
                    
                    
                    return Id;
            
                      
                  }
          
                
                
                
          
                
                
                
                
                
                
                
                
    }
    what the heck am i doing wrong.. i am so confused on why i cannot figure out the login part... i am putting basically all of my information in my main program is that right? or do i start working on the player.java first?

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default

    basketball8533,

    Before going any further, is the application supposed to hold the details of multiple players or just one?

    Regards.

  3. #3
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    Good morning Ronin! thank you for your reply! i am looking for one user being logged in at the time.. and the player.java class must store all of the player information and i am stuck with that.. i have wrote a lot in my main program and i am not using anything from my player.java class because i do not know how?

  4. #4
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    and the player.java class must hold multiple players... stored in a player.txt file... if the players id is stored on that .txt file.. the game is started

  5. #5
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default

    I am still very regarding the requirements for the text file. If you could please answer the following for me then I could assist further:

    1) Is there just a single text file which stores the data for all of the players?
    2) Is the data stored a list of id numbers or does it store all of the data for each player?
    3) Does the file have to be a text file or are you assuming that is the only way of doing it?

    Regarding accessing private variables within a class, all you need are get and set methods.

    Example of set method:

    Java Code:
    public void setRoundsPlayed(int played)
    {
    [INDENT]roundsPlayed = played; // Sets roundsPlayed to played[/INDENT]
    }

    Note that similar methods could be used to increment the value by a set amount instead.

    Java Code:
    public void addRoundsPlayed(int played)
    {
    [INDENT]roundsPlayed += played; // Increments roundsPlayed by played[/INDENT]
    }
    Java Code:
    public void incrementRoundsPlayed()
    {
    [INDENT]roundsPlayed++;  // Increments roundsPlayed by one[/INDENT]
    }


    Example of get method:

    Java Code:
    public int getRoundsPlayed()
    {
    [INDENT]return roundsPlayed;[/INDENT]
    }

    As long as the method is declared public it can be accessed from outside of the class.
    Last edited by Ronin; 10-12-2010 at 02:39 PM. Reason: Comments added.

  6. #6
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    Ronin THANK you so much for your time, you are seriously saving my life... this project is due Thursday and i am losing sleep over it... there is a single file such that called players.txt and they consist of a list of id's...

    This is what the assignment entitles:
    1) To have A file of user ids called "players.txt". This will store one id per line. Each id should have no spaces or other "white space" characters (ex: tabs, new lines), but any other characters are ok. The "players.txt" file will be searched when a player logs in, and when a new player registers his/her id will be appended to the end of this file.
    2) A file for each user containing 4 lines. The lines will be (in order): Last Name, First Name, # of Games Played, # of Games Won. These user files will be named based on user ids. For a given user id, say user1, the file will be named "user1.txt". Note that given an id obtained from the "players.txt" file above, the individual user file can easily be determined and accessed. This user file will be created and initialized when a player first registers, and it will be updated after each game to reflect the new number of games played and games won.

  7. #7
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default

    I'm still not completely clear on the requirements of the assignment but please read the following and tell me if this is correct.

    In the main method the player is asked to input their id.
    A file is then checked to determine if the player id exists. If not then, the main method should request the players details then add the id to the id file and create a player text file.
    An instance of player is created by passing in the player id. The costructor opens the relevant player file and extracts the details.
    Within the main method this data is then altered by calling the methods within the Player class. Each of these methods then update the details within the player file.

    As you are only dealing with a single class for now, the id file should be dealt with by the main method whilst the player file is dealt with by the Player class.

    I'm hoping that this points you in the right direction for your assignment.

    Regards.

  8. #8
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    Ronin, yes that is correct.... there is actually 3 classes.... 1 main, player.java, and scrambler.java (scrambler not concerned right now) my program is actually supposed to make a scrambler game.. where...
    1) Welcome the user and ask if he/she is a new or returning player.

    a) If the player is a returning player, do the following:

    i) Ask him/her to enter his/her id in order to get into the game

    ii) Check to see if the id is in the "players.txt" file. If so, continue on to play the game (number (2) below). If not, have him/her try again. If a player is unsuccessful after 3 tries, the program should end without the game being played.

    b) If the player is a new player, do the following:

    i) Ask him/her to enter his/her name (last and first) and read them in.

    ii) Ask him/her to enter a new id

    iii) Check to see if the id is in the "players.txt" file. If not, append the new id to the end of the file, create the id.txt file for the user, and continue on to play the game (number (2) below). If the id IS in the file, have him/her try again with a different id. Continue this process indefinitely until a valid id has been chosen.

    2) Obtain a random "scramble" word from "words.txt" for the user to guess. Display only the scrambled version, but also store the original for checking. See details on how to implement this in the Implementation Requirements section below.

    3) Ask the user to guess words and compare against the original. Allow at most 5 guesses. If the user guesses the word correctly within the 5 guesses, he/she has won the game. If not, he/she has lost the game.

    4) Notify the user that he/she as won or lost and update his/her file to reflect the outcome of the game.

    I just met with my teacher in office hours... but i am still confused.. i have to make the main program.. the shell of my game where it asks the users questions... and refer back to the player class soo it would look something like this:

    example code he gave us today
    Java Code:
    import java.io.*;
    import java.util.*;
    public class A2Help
    {
        public static void main(String[] args) throws IOException
        {
            Player p = new Player(); // create empty player object
                  
            Scanner inScan = new Scanner(System.in);
            System.out.print("Please enter your id: ");
            String theID = inScan.next();
            p.login(theID, "players.txt");  // Try to log in player based on
                // id provided and file name provided.  If successful, read
                // the rest of the player's data from <id>.txt and set
                // valid to true; otherwise set it to false.
                
            if (p.valid())  // Get state of player -- if valid proceed with game
            {
                Scramble theScramble = new Scramble("words.txt"); 
                    // Create Scramble object reading random word from
                    // words.txt and storing a copy of the word as it is
                    // read and a scrambled copy in instance variables.  This
                    // constructor will probably be fairly long.
                    
                String scram = theScramble.getScrambledWord();
                    // return scrambled copy
                    
                System.out.println("Here is the scrambled word: " + scram);
                System.out.println("What is your guess? ");
                String guess = inScan.next();
                String actual = theScramble.getRealWord();
                    // return unscrambled copy
                    
                if (guess.equals(actual))  // compare users guess
                {
                    System.out.println("Your are right! Good work!");
                    p.wonARound(); // update player - WON
                }
                else
                {
                    System.out.println("Better luck next time");
                    p.lostARound(); // update player - LOST
                }
                System.out.println("Here is your info: \n" + p.toString());
                p.updateFile();  // Save player info back to <id>.txt file
            }
            else
                System.out.println("Sorry, you are not in our files");
            
        }
    }

  9. #9
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default

    It seems straight forward enough now, which but are still confused about?

    Regards.

  10. #10
    cosmothehound is offline Member
    Join Date
    Oct 2010
    Posts
    1
    Rep Power
    0

    Default

    I'm in his class as well. I have no idea where to even start. I do not want to have to withdraw from the class. PLEASE HELP MY LIFE IS GOING TO BE RUINED!.

  11. #11
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default

    Quote Originally Posted by cosmothehound View Post
    I'm in his class as well. I have no idea where to even start.
    The example code for the main class given above would be a good start. Identify the methods required for the Player class and read up on IO streams.

    Regards.

  12. #12
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    Hi Ronin, what i am confused about is the player.java class and building that up... how to i build up the LOGIN part of my player.java class to verify that it is there in my main program? do you get what i mean?

Similar Threads

  1. Vlc player interfaces in java
    By sankar1227 in forum Networking
    Replies: 1
    Last Post: 08-06-2011, 08:39 PM
  2. sound player
    By jperson in forum New To Java
    Replies: 1
    Last Post: 05-02-2010, 05:09 AM
  3. Replies: 0
    Last Post: 11-04-2009, 09:09 PM
  4. Pig Game class plz HELP!!!
    By linux1man in forum New To Java
    Replies: 5
    Last Post: 11-26-2008, 05:28 AM
  5. Developing simple MP3 player in Java....
    By JavaEmpires in forum New To Java
    Replies: 0
    Last Post: 01-16-2008, 10:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •