Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    suneko is offline Member
    Join Date
    May 2010
    Posts
    19
    Rep Power
    0

    Default Scrabble-like game

    HELLO! This is 巣猫(Suneko) here.

    I've going through some problems trying to create my scrabble like game in JAVA. So far right, I've managed to output random letters using arrays and stuff. So right now, my problem is since this is a two player game, I'm able to make it take turns to player 1 and player 2 but however, my problem comes when one of the players want to skip their turn, so somehow, it does not continue smoothly. So there would be an error that I might not be able to see, so I hope I can have some help!

    Also, is there a way to match the user's input to my random letter arrays? I've not figured out how to. :(

    Code:
    Java Code:
    import java.util.Random;
    import java.util.Scanner;
    
    public class WordGameAssignment1 {
    	
    	public static void main(String[] args) {
    		Random rand = new Random();
    		Scanner userInput = new Scanner(System.in);
    		
    		//==================Player object===============================================
    		Players playerOne = new Players();
    		playerOne.score = 0;
    		playerOne.choice = "blah";
    		playerOne.turn = true;
    		
    		Players playerTwo = new Players();
    		playerTwo.score = 0;
    		playerTwo.choice = "blah";
    		playerTwo.turn = false;
    		
    		//==============================================================================
    		
    		//================== Alphabet ==================================================
    		String[] newChars = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
    				"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
    		}; //values of the 26 alphabets to be used
    		
    		System.out.println("FINDYOURWORDS\n");
    		
    		int[] arrayRandom = new int[10]; //int array for word limiter
    		String[] randomLetter = new String[10]; //storing the letters in newChars into this array
    		
    		//===============================================================================
    		
    		boolean cont = true;
    		
    		while (cont) {
    			
    			if (playerOne.turn) {
    				System.out.print("Letters of Player 1: ");
    			}
    			else if (!playerOne.turn) {
    				System.out.print("Letters of Player 2: ");
    			}
    			
    			for (int i = 0; i < arrayRandom.length; i++) { //running through the array limiter
    			int r = rand.nextInt(newChars.length); //assigning random nums to the array of letters
    			randomLetter[i] = newChars[r];
    			System.out.print(randomLetter[i]+ " ");
    			
    			}
    		
    			//input section for player
    		
    			System.out.println("");
    			System.out.println("Enter your word (or '@' to pass or '!' to quit): ");
    			if (playerOne.turn) {
    				playerOne.choice = userInput.next();
    				System.out.println(playerOne.turn);
    				playerOne.turn = false;
    			}
    			else if (!playerOne.turn){
    				playerTwo.choice = userInput.next();
    				System.out.println(playerOne.turn);
    				playerOne.turn = true;
    			}
    			//System.out.println(choice);
    
    		
    			String[] wordList = FileUtil.readDictFromFile("words.txt"); //Still dunno what this is for
    			
    			
    			if (playerOne.choice.equals("@")) {
    				playerOne.turn = false;
    			}
    			else if (playerTwo.choice.equals("@")) {
    				playerOne.turn = true;
    			}
    			else if (playerOne.choice.equals("!")) {
    				cont = false;
    			}
    			
    			
    			for (int i = 0; i < wordList.length; i++) {
    			//System.out.println(wordList[i]);
    				if (playerOne.choice.equalsIgnoreCase(wordList[i]) || playerTwo.choice.equalsIgnoreCase(wordList[i])){
    
    		}
    
    			}
    		
    		
    		
    		
    
    		}
    	}}
    I know there is a couple of stuff that is unnecessary but I'm trying for hours to make it right. So I need a nudge in the right direction.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I'm wondering if your current problem is a symptom of more serious program design issues as you seem to be trying to cram a lot of logic and code inside of a poor main method. I'm sorry for not answering your direct question, but I would completely rewrite this code to allow for more classes and more modularity. I'm thinking that this game should be flexible enough for multiple players, and so the last thing I'd want to do would be to hard-code stuff for player1 and player2. For one, I'd give my code a Board object that represents the scrabble board and that holds a 2-dimensional array of char. I don't see that you have this anywhere (perhaps you do, but are just not showing us). Consider also a Tile class that holds a char and a numeric value (since each letter has an innate value) and giving Player an ArrayList of Tiles. I would also create a Game class that manages the interactions between these objects and controls the "turns". It could hold an ArrayList of Player and iterate through that list for each turn. I'd write the code to be as UI-agnostic as possible with an eye towards eventually making it a GUI game (but not now!). As an aside, I'd also not want to read from the dictionary file ("words.txt") inside of the game loop but just once at the beginning of the program.

    Much luck and welcome to the forum!

  3. #3
    suneko is offline Member
    Join Date
    May 2010
    Posts
    19
    Rep Power
    0

    Default

    Oh my goodness. That's pretty too much for me to understand fully in one go. Right now the game is similar to scrabble, its just a simple text game where two players get 10 randomly generated letters each turn and they have to make a word out of the random letters to score point, yeah.

    So my problem is that, I've just started to learn Java and the very basics, so my problem now is yeah, I could be cramming a lot of logic into the poor method body. So if I wanted for the players to take turns, I should put the two players into a turn array and manipulate using the array?

    Also, I'm not practically sure on how do I match whatever input the players put in with their randomly generated letters and with the words from the dictionary word.txt.

    >:

    Any suggestions on how to code it much more efficiently?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Oops, my bad. I took your "Scrabble-like" and assumed that it was Scrabble in all it's gory details.

    First of all, I would change the name of the Players class to Player as it encapsulates the state and behavior of a single player (sorry for being a semantic nit-picker, but it's just my nature). You could then have an array of Player[] say called players and a method called turn(Player), and this could simplify your game loop to be something like:

    Java Code:
    while (cont)  {
      for (Player player : players) {
        turn(player);
      }
    }
    where turn gets the Player's input, checks if the player wants to quit and if so sets cont to false (cont has to be a static class field for this to work), and sets the Player's score. If the Player wants to skip a turn, simply return from the method.

  5. #5
    suneko is offline Member
    Join Date
    May 2010
    Posts
    19
    Rep Power
    0

    Default

    Ah that's a little too advanced for me. But this is how I wrote it so far and it does transient from player 1 to player 2 smoothly. If you're interested in the code.

    Java Code:
    while (cont) {
    			if (turn == 1) {
    				System.out.print("Letters for Player 1: ");
    			}
    			else {
    				System.out.print("Letters for Player 2: ");
    			}
    			
    			for (int i = 0; i < randomNumber.length; i++) {
    				int r = rand.nextInt(alphabets.length);
    				randomLetter[i] = alphabets[r];
    				System.out.print(randomLetter[i]+ " ");
    		}
    			
    			
    			//User interaction
    			System.out.println("");
    			System.out.println("Enter your word (or '@' to pass or '!' to quit): ");
    			if (turn == 1) {
    				playerInput = userInput.nextLine();
    				turn++;
    			}
    			else {
    				playerInput = userInput.nextLine();
    				turn--;
    			}
    Last edited by suneko; 05-23-2010 at 05:41 PM. Reason: Solved what I needed to do.

  6. #6
    Tissue is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default

    Hi, im sorry but can i know what's th code that def your 'turn' in
    " if (turn == 1) {
    playerInput = userInput.nextLine();
    turn++;
    } "
    ? :)

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,608
    Rep Power
    23

    Default

    Instead of an int with ++ and --, if your code only allows 2 players you could use a boolean. Or just set the int to 0 or 1 vs ++ or -- to toggle between the two players.
    Using ++ and -- implies that there are more than two possible values for the int.

  8. #8
    suneko is offline Member
    Join Date
    May 2010
    Posts
    19
    Rep Power
    0

    Default

    I've completely re-written my codes.

    I can't put another if condition within my for which is within my while loop. So I'm just depending on my while loop because I made the mistake of putting in two independant conditions which makes the whole program screw up, so I've decided to take it out and put the main changing turns in my program at where the user has to input something.

    :D

    Now my problem is trying to match my array of random letters to what the user has input. Would anyone have any idea on how to do so?

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,608
    Rep Power
    23

    Default

    match my array of random letters to what the user has input
    Are the letters single letters, eg A or J or Q or are they Strings: AVD or JKO
    Are they limited to the 26 letters in our alphabet?
    What happens when the user enters one of the random letters?

    A solution: Save all the letters concatenated in a String and use the indexOf() method to detect if a user entered letter is in the string.

  10. #10
    suneko is offline Member
    Join Date
    May 2010
    Posts
    19
    Rep Power
    0

    Default

    Are the letters single letters, eg A or J or Q or are they Strings: AVD or JKO
    Are they limited to the 26 letters in our alphabet?
    What happens when the user enters one of the random letters?

    A solution: Save all the letters concatenated in a String and use the indexOf() method to detect if a user entered letter is in the string.
    The array keeps all of them in a single letters and are using the char data type. And yes it is limited to only 26 letters of our alphabets. What should happen when the user enters one of the random letters is that it will proceed to give them a point since they are using their own designated random letters, however they're not allowed to use other letters that they don't have during the turn.

    I'm not familiar with the methods section since I haven't really learnt it in school and practiced it to success yet. So I'm wondering are there other simple ways to do this?

    Thanks.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,608
    Rep Power
    23

    Default

    not familiar with the methods section
    You will have to understand how to use various classes and their methods. Without that understanding, you will NOT be able to write many programs. Very simple programs can be written without classes.
    If you must store the letters in an array, then you'll need to use a loop to search that array for a match with the new letter.
    If you can build a String with all the letters, then the indexOf() method could be used to determine if the new letter is in the String.

  12. #12
    suneko is offline Member
    Join Date
    May 2010
    Posts
    19
    Rep Power
    0

    Default

    Alright, I've begun to use methods, however, there seem to be something which I'm overlooking. Some how the logic is not really working properly.

    Please have a look because when the player does '@' it's supposed to output
    their word score and player score, but it totally doesn't do that, but so far everything else is coming into shape.

    I use one big loop called cont.

    Java Code:
           while (cont) {
                    //stuff to generate player thingies
    .
    .
    .
    			if (user.equals("!")) {
    				cont = false;
    				break;
    			}
    			else if (user.equals("@")) {
    				player = player + 1;
    				System.out.println(" ");
    			}
    			else if (matchedWord(user) && matchedLetter(user,randomly)) {
    				for (int i = 0; i < letters.length; i++) {	// need to loop through two arrays
    					for (int x = 0; x < user.length(); x++) {
    						if (user.charAt(x) == letters[i]) {
    							wordScore = wordScore + letterPoints[i];
    						}
    					}
    				}
    				
    				System.out.println("Total word score: " + wordScore);
    				if (player%2 == 0) {
    					player1score = player1score + wordScore;
    					System.out.println("Total score for player 1: " + player1score);
    			}
    				else {
    					player2score = player2score + wordScore;
    					System.out.println("Total score for player 2: " + player2score);
    			}
    				wordScore = 0;
    				player = player + 1;
    	}
    		
    }
    So that's my code for the points and stuffs. The player variable is controlling the turns and such and I use the % to alternate between the two players player 1 is even and player 2 is odd.

    So the problem is that is as I've stated right above, everything is fine, except when the player skips their turn/'@', it does not out put their word score or player score, it just proceeds to the next player.

    So things that I've tried is that I used another while loop for my if statements, but when it tries to skip, it continues to loop forever. I could need some more help in my design and also with the checking of my codes.

    Thanks.

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,608
    Rep Power
    23

    Default

    If the program is not reading the input correctly, you need to debug the code by
    Adding some println() statements to show the value of the user and player variables.

    when the player skips their turn/'@', it does not out put their word score or player score,
    Where are those values displayed?
    Last edited by Norm; 05-28-2010 at 03:22 PM.

  14. #14
    suneko is offline Member
    Join Date
    May 2010
    Posts
    19
    Rep Power
    0

    Default

    Clearly now my game is able to do word and letter checks and assign scoring and whatsoever, however it's unable to skip turns to the next player. I've place it in a loop, but when @ is input, where it's supposed to do switch players, it either continues to loop forever or it will just skip to the next player without outputting their scores as 0. -___-
    Last edited by suneko; 05-28-2010 at 08:33 PM. Reason: Solved

  15. #15
    suneko is offline Member
    Join Date
    May 2010
    Posts
    19
    Rep Power
    0

    Default

    This is the output right now:

    Find your words
    Letters for player 1: s c n s q o c o k p
    Enter your word (or '@' to pass or '!' to quit):
    con
    Total word score: 7
    Total score for player 1: 7

    Letters for player 2: a l e j c q r d j p
    Enter your word (or '@' to pass or '!' to quit):
    eject
    Error : A valid word is formed but one or more letter(s) used are not yours
    Enter your word (or ‘@’ to pass or ‘!’ to quit) :
    ale
    Total word score: 7
    Total score for player 2: 7

    Letters for player 1: u y g a d s t m e g
    Enter your word (or '@' to pass or '!' to quit):
    @
    !
    What would it mean when nothing happens? :/ Clearly something was supposed to happen...

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,608
    Rep Power
    23

    Default

    Do your println()s show what the user entered each time? What is shown when the user enters the @?

    Add some text to the println() to label what is being displayed. Don't just show the value.
    For example: System.out.println("user input=" + user + "<");
    I can't tell from your post what the output is.

  17. #17
    suneko is offline Member
    Join Date
    May 2010
    Posts
    19
    Rep Power
    0

    Default

    Actually, the loop was supposed to loop through anyways when the player enters something, that's how it was basically designed. There is nothing at all. It just doesn't enter, so either I needa put some .nextLine(); to get the enter character away or something, I'm trying to fix while I experiment why it wasn't working well.

    I'll come again soon to see if I've solved my problem.

  18. #18
    suneko is offline Member
    Join Date
    May 2010
    Posts
    19
    Rep Power
    0

    Default

    Yeah alright! The whole program is done and is working to how it's supposed to be. :D

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,022
    Blog Entries
    7
    Rep Power
    20

    Default

    The last two posters are trying to cheat; I urge the OP not to post his/her final code.

    kind regards,

    Jos

  20. #20
    Jubic is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  2. game code for any game
    By deathnote202 in forum Java Gaming
    Replies: 4
    Last Post: 06-10-2010, 08:06 AM
  3. Help with game
    By TGH in forum New To Java
    Replies: 26
    Last Post: 01-12-2010, 02:55 PM
  4. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 06:00 AM
  5. game
    By amith in forum AWT / Swing
    Replies: 0
    Last Post: 05-19-2008, 05:16 PM

Posting Permissions

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