Results 1 to 4 of 4
  1. #1
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    148
    Rep Power
    0

    Default Card Game Feedback

    NOTE: If you don't have the time and will to look into a "large" program, then don't read this :)

    Hello!

    This program down below is a card game conveniently named "21", where you compete against the computer to get as close to 21 points as possible in cards. The cards are worth of points equivalent to their normal ranking; kings give 13 points, ladies 12 and sevens 7, etc. There normally are more than two players, but since I'm not that talented at computer programming yet, two players are almost more than enough xD The players, one at a time, are asked if they want to draw a card, and they have to choose if they want to do that or not. If all of the players stop, the players with points over 21 automatically lose, and the player with points under or equal to 21 wins!

    It seems like kind of a pointless card game to you? To me too, but I couldn't program anything more complicated like Hearts or Poker, because that would've taken weeks even if I had the skills. So, to my "questions":

    1. I still haven't figured out how to change turns after a player has drawn a card, in other words, how to let another player (the computer or the human) draw a card. Have you got any ideas on how to do it?

    2. The first player who draws a card's cards are visible, but the second player's cards aren't visible in the cards printCardDeck method. How do I fix that?

    3. Have you got any feedback on what I could improve and/or change to shorten and/or enhance the program?

    Reading and helping me out with this would really mean a lot to me, I would be eternally grateful! Okay, not really, but I would be immensely thankful!!! :)

    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    import java.util.Vector;
    import java.util.Arrays;
    import java.io.*; 
    
    public class Tjugoett{
    	public static void main(String[] args) throws IOException{
    		Scanner hej = new Scanner(System.in);
    		char c = askTwoParameterQuestion("Do you want to play or quit? (P/Q): ", 'P', 'Q');
    		int turn;
    		Vector<String> cards = new Vector<String>();
    		cards.addAll(Arrays.asList("\u2664 E", "\u2664 2", "\u2664 3", "\u2664 4", "\u2664 5", "\u2664 6", "\u2664 7", "\u2664 8", "\u2664 9", "\u2664 10", "\u2664 Knight", "\u2664 Queen", "\u2664 King",
    								   "\u2665 E", "\u2665 2", "\u2665 3", "\u2665 4", "\u2665 5", "\u2665 6", "\u2665 7", "\u2665 8", "\u2665 9", "\u2665 10", "\u2665 Knight", "\u2665 Queen", "\u2665 King",
    								   "\u2666 E", "\u2666 2", "\u2666 3", "\u2666 4", "\u2666 5", "\u2666 6", "\u2666 7", "\u2666 8", "\u2666 9", "\u2666 10", "\u2666 Knight", "\u2666 Queen", "\u2666 King",
    								   "\u2667 E", "\u2667 2", "\u2667 3", "\u2667 4", "\u2667 5", "\u2667 6", "\u2667 7", "\u2667 8", "\u2667 9", "\u2667 10", "\u2667 Knight", "\u2667 Queen", "\u2667 King"));
    		boolean mainLoop = c == 'P' ? true : false;
    		while(mainLoop){
    			
    			int takenCards[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    		            		    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    		                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    		                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    			cards = shufflingCardDeck(cards);
    			boolean gameRunning = true;
    			int x = 0, points1 = 0, points2 = 0, initializedCards1 = 0, initializedCards2 = 0, victories1 = 0, victories2 = 0;
    			c = askTwoParameterQuestion("Do you want to start dealing? (Y/N): ", 'Y', 'N');
    			turn = c == 'Y' ? 0 : 1;
    			
    			while(gameRunning){
    				
    				boolean roundSpecifier = true, start = true;
    				int loss = 0;
    				int startingPoint = turn;
    				while(roundSpecifier){
    					printCardDeck(cards, takenCards, points1, points2, initializedCards1, initializedCards2);
    					
    					if(turn % 2 == 0){
    						
    						c = askTwoParameterQuestion("Do you want to draw a card or stop? (D/S): ", 'D', 'S');
    						start = c == 'D' ? true : false;
    						
    						if(start){
    							takenCards[x] = 2;
    							System.out.print("\nYou drew " + cards.get(x) + "!");
    							System.in.read();
    							initializedCards1++;
    							x++;
    							points1 = calculatePoints(cards, takenCards, points1);
    						}
    						
    						else if(!start){
    							System.out.println("\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
    							System.in.read();
    						}
    						
    					}else if(turn % 2 != 0){
    						takenCards[x] = 1;
    						System.out.print("The computer drew " + cards.get(x));
    						System.in.read();
    						initializedCards2++;
    						x++;
    						points2 = calculatePoints(cards, takenCards, points2);
    					}
    					
    					loss = controlPoints(points1, points2, turn, startingPoint);
    					
    					if(loss == 2){
    						victories1++;
    						roundSpecifier = false;
    					}
    					
    					else if(loss == 1){
    						victories2++;
    						roundSpecifier = false;
    					}
    					turn++;
    				}
    				
    				if(victories1 == 1 || victories2 == 1)
    					gameRunning = false;
    			}
    			
    			if(victories1 > victories2)
    				System.out.println("You won!");
    			
    			else if(victories2 > victories1)
    				System.out.println("The computer won!");
    			c = askTwoParameterQuestion("Do you want to play again? (Y/N): ", 'Y', 'N');
    			mainLoop = c == 'Y' ? true : false;
    		}
    		
    		System.out.print("Goodbye!");
    	}
    	
    	static int controlPoints(int points1, int points2, int turn, int startingPoint){
    		int loss = 0;
    		for(int x=1;x<=5;x+=2){
    			if(turn == startingPoint + x && points2 > 14){
    				if(points1 > points2){
    					loss = 2;
    				}
    				else if(points1 < points2){
    					loss = 1;
    				}
    			}
    		}
    		return loss;
    	}
    	
    	static int calculatePoints(Vector<String> cards, int[] test, int points){
    		for(int x=0;x<cards.size();x++){
    			if(test[x] == 1 || test[x] == 2){
    				if(cards.get(x).indexOf('E') != -1)
    					points += 1;
    				else if(cards.get(x).indexOf('2') != -1)
    					points += 2;
    				else if(cards.get(x).indexOf('3') != -1)
    					points += 3;
    				else if(cards.get(x).indexOf('4') != -1)
    					points += 4;
    				else if(cards.get(x).indexOf('5') != -1)
    					points += 5;
    				else if(cards.get(x).indexOf('6') != -1)
    					points += 6;
    				else if(cards.get(x).indexOf('7') != -1)
    					points += 7;
    				else if(cards.get(x).indexOf('8') != -1)
    					points += 8;
    				else if(cards.get(x).indexOf('9') != -1)
    					points += 9;
    				else if(cards.get(x).indexOf("10") != -1)
    					points += 10;
    				else if(cards.get(x).indexOf("Knight") != -1)
    					points += 11;
    				else if(cards.get(x).indexOf("Queen") != -1)
    					points += 12;
    				else if(cards.get(x).indexOf("King") != -1)
    					points += 13;
    			}
    			if(test[x] == 1)
    				test[x] = 3;
    			else if(test[x] == 2)
    				test[x] = 4;
    		}
    		return points;
    	}
    	
    	static void printCardDeck(Vector<String> cards, int takenCards[], int points1, int points2, int initializedCards1, int initializedCards2){
    		System.out.println("\nThe computer's cards:");
    		for(int x=0;x<cards.size();x++){
    			if(takenCards[x] == 1 || takenCards[x] == 3){
    				if(initializedCards2 - x == 1)
    					System.out.println(cards.get(x) + "!");
    				else if(initializedCards2 - x > 1)
    					System.out.print(cards.get(x) + ", ");
    			}
    		}
    		System.out.println("Your cards:");
    		for(int x=0;x<cards.size();x++){
    			if(takenCards[x] == 2 || takenCards[x] == 4){
    				if(initializedCards1 - x == 1)
    					System.out.println(cards.get(x) + "!");
    				else if(initializedCards1 - x > 1)
    					System.out.print(cards.get(x) + ", ");
    			}
    		}
    		System.out.println("The computer's points: " + points2 + "\nYour points: " + points1 + "\n");
    	}
    	
    	static Vector<String> shufflingCardDeck(Vector<String> cards){
    		Random generator = new Random();
    		for(int x=0;x<cards.size();x++){
    			int random = generator.nextInt(cards.size());
    			String s = cards.get(random);
    			cards.set(random, cards.get(x));
    			cards.set(x, s);
    		}
    		return cards;
    	}
    	
    	static char askTwoParameterQuestion(String question, char alternative1, char alternative2){
    		Scanner hej = new Scanner(System.in);
    		char c = ' ';
    		while(c != alternative1 && c != alternative2){
    			System.out.print(question);
    			c = hej.next().charAt(0);
    			c = capitalizingLetters(c);
    		}
    		return c;
    	}
    	
    	static char capitalizingLetters(char test){
    		if(test >= 97 && test <= 122)
    			test -= 32;
    		return test;
    	}
    }
    Last edited by Zelaine; 09-21-2013 at 07:53 PM.

  2. #2
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    107
    Rep Power
    0

    Default Re: Card Game Feedback

    What I've done to program games before is to have the following classes:

    Java Code:
    // an abstract class
    abstract class Player {
    }
    
    // a concrete subclass of the Player class
    class HumanPlayer extends Player {
    }
    
    // another concrete subclass of the Player class
    class ComputerPlayer extends Player {
    }
    A Game class with one game object which has a Player object for each player.

    To the game object, it has references to object typed as Player objects which are really either a human player or a computer player, but the code in the Game class can treat the players in an abstract manner, using the abstract class called "Player".

    You can email me if you would like to.

    -- Kaydell
    kaydell@yahoo.com
    Kaydell Leavitt | Home
    Last edited by kaydell2; 09-26-2013 at 06:19 PM.

  3. #3
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    148
    Rep Power
    0

    Default Re: Card Game Feedback

    I managed to solve the problems that I had, but my program is still very long and not so efficient in code, but I guess these objects could facilitate writing the program and probably shorten it a lot. I'm afraid I haven't learned about objects yet though, but I will look into it as soon as I get the chance.

    Thanks for the tip by the way, even though I do not fully understand it xD

    Just a question, in what ways is an abstract class different from a regular one?

  4. #4
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    107
    Rep Power
    0

    Default Re: Card Game Feedback

    Quote Originally Posted by Zelaine View Post
    I managed to solve the problems that I had, but my program is still very long and not so efficient in code, but I guess these objects could facilitate writing the program and probably shorten it a lot.
    Object tend to make the code easier to understand and more generally useful. Maybe shorter too.

    I'm afraid I haven't learned about objects yet though, but I will look into it as soon as I get the chance.
    OK, good. Object-oriented programming is very important nowaday.

    Thanks for the tip by the way, even though I do not fully understand it xD
    I was fooling around writing a chess program and I was trying to figure out how to let the human player play either the white pieces or the black pieces, or let two humans play and have the chess program be the referee, or how to have the chess program play itsself.

    The implementation of having the chess game object have two player objects and letting each of these player objects be either a computer player or a human player in any combination made this all easy.

    The game object would just call the following method:

    Java Code:
    public abstract Move askMove();
    The human player class would have one implementation of the method and the computer player class would have a different implementation of the method. The code in the Game class doesn't have to know whether the move was made by a human or by the computer. It would just get the Move object from a Player object.

    When you use object-oriented programming and when you have a good design, things start to fall into place.

    Just a question, in what ways is an abstract class different from a regular one?
    An object cannot be created from an abstract class. So what good is an abstract class?

    Each object has two types associated with it:

    1. The class that it is really from.
    2. The type that it is referred to as.

    The class that an object really is, is always how it was created.

    Player player = new HumanPlayer();

    In the code above, there is an object-reference variable called "player" whose type is "Player" which is how the object is referrred to.

    Also in the code above, the actual object is created from the class called "HumanPlayer". This is the class that this object really is and will always be, but it can be referred to as a Player object because HumanPlayer is a subclass of Player. HumanPlayer is a kind of Player.

    So, an abstract class, such as the Player class can be used as "abstractions".

    You can post a reply or email me. (I like to answer questions about programming.)

    -- Kaydell
    kaydell@yahoo.com
    http://java-coaching/contact
    Last edited by kaydell2; 09-27-2013 at 04:19 AM.

Similar Threads

  1. looking for feedback on random card generator
    By Russd772 in forum New To Java
    Replies: 8
    Last Post: 12-28-2012, 05:06 AM
  2. FEEDBACK: Simple Game
    By mine0926 in forum Java Gaming
    Replies: 5
    Last Post: 07-24-2011, 02:50 PM
  3. Replies: 8
    Last Post: 09-23-2010, 12:48 AM
  4. Card Game
    By abby0910 in forum New To Java
    Replies: 1
    Last Post: 07-24-2010, 12:38 AM
  5. I need feedback on my TicTacToe game
    By kiregad in forum New To Java
    Replies: 4
    Last Post: 03-21-2010, 10:09 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
  •