Results 1 to 20 of 21
Thread: Scrabble-like game
- 05-23-2010, 12:11 PM #1
Member
- Join Date
- May 2010
- Posts
- 19
- Rep Power
- 0
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:
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.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'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!
- 05-23-2010, 02:29 PM #3
Member
- Join Date
- May 2010
- Posts
- 19
- Rep Power
- 0
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?
-
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:
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.Java Code:while (cont) { for (Player player : players) { turn(player); } }
- 05-23-2010, 05:37 PM #5
Member
- Join Date
- May 2010
- Posts
- 19
- Rep Power
- 0
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.
- 05-26-2010, 12:38 PM #6
Member
- Join Date
- May 2010
- Posts
- 1
- Rep Power
- 0
Hi, im sorry but can i know what's th code that def your 'turn' in
" if (turn == 1) {
playerInput = userInput.nextLine();
turn++;
} "
? :)
- 05-26-2010, 04:56 PM #7
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.
- 05-26-2010, 08:31 PM #8
Member
- Join Date
- May 2010
- Posts
- 19
- Rep Power
- 0
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?
- 05-26-2010, 08:48 PM #9
Are the letters single letters, eg A or J or Q or are they Strings: AVD or JKOmatch my array of random letters to what the user has input
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.
- 05-27-2010, 07:07 AM #10
Member
- Join Date
- May 2010
- Posts
- 19
- Rep Power
- 0
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.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.
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.
- 05-27-2010, 03:14 PM #11
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.not familiar with the methods section
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.
- 05-28-2010, 10:50 AM #12
Member
- Join Date
- May 2010
- Posts
- 19
- Rep Power
- 0
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.
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.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 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.
- 05-28-2010, 03:19 PM #13
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.
Where are those values displayed?when the player skips their turn/'@', it does not out put their word score or player score,Last edited by Norm; 05-28-2010 at 03:22 PM.
- 05-28-2010, 05:45 PM #14
Member
- Join Date
- May 2010
- Posts
- 19
- Rep Power
- 0
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
- 05-28-2010, 08:40 PM #15
Member
- Join Date
- May 2010
- Posts
- 19
- Rep Power
- 0
This is the output right now:
What would it mean when nothing happens? :/ Clearly something was supposed to happen...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):
@
!
- 05-28-2010, 11:22 PM #16
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.
- 05-29-2010, 05:19 PM #17
Member
- Join Date
- May 2010
- Posts
- 19
- Rep Power
- 0
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.
- 05-29-2010, 05:43 PM #18
Member
- Join Date
- May 2010
- Posts
- 19
- Rep Power
- 0
Yeah alright! The whole program is done and is working to how it's supposed to be. :D
- 05-30-2010, 09:48 AM #19
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
The last two posters are trying to cheat; I urge the OP not to post his/her final code.
kind regards,
Jos
- 05-30-2010, 09:51 AM #20
Member
- Join Date
- May 2010
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
Implementing "Game Over" in Minesweeper game based on Gridworld framework.
By JFlash in forum New To JavaReplies: 2Last Post: 08-05-2010, 04:49 AM -
game code for any game
By deathnote202 in forum Java GamingReplies: 4Last Post: 06-10-2010, 08:06 AM -
Help with game
By TGH in forum New To JavaReplies: 26Last Post: 01-12-2010, 02:55 PM -
2D strategy game or 2D war game
By led1433 in forum Java 2DReplies: 5Last Post: 02-10-2009, 06:00 AM -
game
By amith in forum AWT / SwingReplies: 0Last Post: 05-19-2008, 05:16 PM


LinkBack URL
About LinkBacks

Bookmarks