Page 1 of 2 12 LastLast
Results 1 to 20 of 26

Thread: The TWC Programming Challenge

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    fatsheep's Avatar Civitate
    Join Date
    Aug 2004
    Location
    Somewhere
    Posts
    1,931

    Default The TWC Programming Challenge

    I've taken the liberty of making the first TWC programming challenge. Since I really have no idea how many entires to expect or the caliber of the programmers here, let's try something rather simple: a rock, paper, scissors game. It doesn't have to be fancy - just a text based menu with a computer to play against. Helow is my example (written in python). Good luck on your entries!

    Code:
    #!/usr/bin/python
    
    ## IMPORTS ##
    import random
    
    ## CONSTANTS ##
    ROCK = "ROCK"
    SCISSORS = "SCISSORS"
    PAPER = "PAPER"
    QUIT = "QUIT"
    
    DRAW = "DRAW"
    WIN = "WIN"
    LOSE = "LOSE"
    
    PROMPT = "Choose (R)ock, (P)aper, or (Scissors): "
    
    ## DICTIONARIES AND LISTS ##
    conv = { 'R' : ROCK, 'P' : PAPER, 'S' : SCISSORS, 'Q' : QUIT }
    num_conv = { 1 : ROCK, 2 : PAPER, 3 : SCISSORS }
    # human + computer 
    score = { ROCK+PAPER: LOSE, ROCK+ROCK: DRAW, ROCK+SCISSORS: WIN,
    	  PAPER+ROCK: WIN, PAPER+PAPER: DRAW, PAPER+SCISSORS: LOSE,
    	  SCISSORS+ROCK: LOSE, SCISSORS+SCISSORS: DRAW, SCISSORS+PAPER: WIN }
    
    ### FUNCTONS ###
    
    def print_records(humanW, compW):
    	print "----- RECORDS -----"
    	print "Human: %s - %s, Computer: %s - %s" % (humanW, compW, compW, humanW)
    	print ""	
    
    ## VARIABLE DECLARATIONS ##
    human = 0
    humanW = 0
    compW = 0
    while True:
    	try:
    		human = conv[raw_input(PROMPT).upper()]
    	except:
    		print "Error: Invalid input!\n"
    		continue
    
    	if human == QUIT:
    		break
    
    	computer = num_conv[random.randint(1,3)]
    	print "Human: %s\t" % human,
    	print "Computer: %s\t" % computer,
    	results = score[human+computer]
    	
    	
    	if results == WIN:
    		humanW += 1
    		print "Human wins!"
    	elif results == LOSE:
    		compW += 1
    		print "Computer wins!"
    	else:
    		print "Draw!"
    	print_records(humanW, compW)
    Some sample output:

    Choose (R)ock, (P)aper, or (Scissors): r
    Human: ROCK Computer: SCISSORS Human wins!
    ----- RECORDS -----
    Human: 1 - 0, Computer: 0 - 1

    Choose (R)ock, (P)aper, or (Scissors): p
    Human: PAPER Computer: SCISSORS Computer wins!
    ----- RECORDS -----
    Human: 1 - 1, Computer: 1 - 1

    Choose (R)ock, (P)aper, or (Scissors): s
    Human: SCISSORS Computer: ROCK Computer wins!
    ----- RECORDS -----
    Human: 1 - 2, Computer: 2 - 1

    Choose (R)ock, (P)aper, or (Scissors): s
    Human: SCISSORS Computer: ROCK Computer wins!
    ----- RECORDS -----
    Human: 1 - 3, Computer: 3 - 1

    Choose (R)ock, (P)aper, or (Scissors):
    Last edited by fatsheep; March 18, 2007 at 09:01 PM.

  2. #2

    Default Re: The TWC Programming Challenge

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <stdio.h>
    using namespace std;
    
    int main()
    {
    	int player_win=0;
    	int computer_win=0;
    	
    	char player_move;
    	char computer_move;
    	int random;
    	do{
    		cout << "Choose (R)ock, (P)aper, or (S)cissors): ";
    		cin >> choice;
    		random=rand()%3;
    		if(random==0)
    			computer_move='R';
    		else if(random==1)
    			computer_move='P';
    		else if(random==2)
    			computer_move='S';
    		if( (player_move=='S' && computer_move=='R') ||
    			(player_move=='R' && computer_move=='P') ||
    			(player_move=='P' && computer_move=='S') )
    			{
    				computer_win++;
    				cout << "Computer wins!" << endl;
    				cout << "Human " << player_win<< "Computer " << computer_win << endl;
    			}
    		else if( (player_move=='R' && computer_move=='S') ||
    			(player_move=='P' && computer_move=='R') ||
    			(player_move=='S' && computer_move=='P') )
    			{
    				player_win++;
    				cout << "Human wins!" << endl;
    				cout << "Human " << player_win<< "Computer " << computer_win << endl;
    			}
    		else
    		{
    			cout << "TIED" << endl; 
    			cout << "Human " << player_win<< "Computer " << computer_win << endl;
    		}
    	while(1);
    	return 0;
    }

  3. #3
    fatsheep's Avatar Civitate
    Join Date
    Aug 2004
    Location
    Somewhere
    Posts
    1,931

    Default Re: The TWC Programming Challenge

    Lee, you missed the ending bracket in your 'do' block and the compiler didn't recognize the variable 'choice'. I'm assuming you meant to use the integer 'player_move' instead so I substituted that in as well:

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <stdio.h>
    using namespace std;
    
    int main()
    {
    	int player_win=0;
    	int computer_win=0;
    	
    	char player_move;
    	char computer_move;
    	int random;
    	do 
    	{
    		cout << "Choose (R)ock, (P)aper, or (S)cissors): ";
    		cin >> player_move;
    		random=rand()%3;
    		if(random==0)
    			computer_move='R';
    		else if(random==1)
    			computer_move='P';
    		else if(random==2)
    			computer_move='S';
    		if( (player_move=='S' && computer_move=='R') ||
    			(player_move=='R' && computer_move=='P') ||
    			(player_move=='P' && computer_move=='S') )
    			{
    				computer_win++;
    				cout << "Computer wins!" << endl;
    				cout << "Human " << player_win<< "Computer " << computer_win << endl;
    			}
    		else if( (player_move=='R' && computer_move=='S') ||
    			(player_move=='P' && computer_move=='R') ||
    			(player_move=='S' && computer_move=='P') )
    			{
    				player_win++;
    				cout << "Human wins!" << endl;
    				cout << "Human " << player_win<< "Computer " << computer_win << endl;
    			}
    		else
    		{
    			cout << "TIED" << endl; 
    			cout << "Human " << player_win<< "Computer " << computer_win << endl;
    		} 
    	}
    	while(1);
    	return 0;
    }
    With the above code I got your program to compile however, the output isn't what you'd expect:

    ubuntu@ubuntu:~/Desktop$ ./rps
    Choose (R)ock, (P)aper, or (S)cissors): r
    TIED
    Human 0Computer 0
    Choose (R)ock, (P)aper, or (S)cissors): p
    TIED
    Human 0Computer 0
    Choose (R)ock, (P)aper, or (S)cissors): s
    TIED
    Human 0Computer 0
    Choose (R)ock, (P)aper, or (S)cissors): p
    TIED
    Human 0Computer 0
    Choose (R)ock, (P)aper, or (S)cissors): rp
    TIED
    ---------------------------------------


    Incinerate, you sure did miss a lot of semicolons . Here's a few little reminders about C++:

    1. In order to use std::cin and std::cout you have to include the library that contains them. You need this line at the beginning of your program
      #include <iostream>
    2. In order to refer to std::cin as cin and std::cout as cout you need to include the line
      using namespace std;
      . Otherwise the compiler has no idea what you're talking about.
    3. Remember to enclose characters in single quotes. For example, instead of
      if(input = a)
      you need
      if(input = 'a')
      . The former will not compile.
    4. If you need to have a command that spans multiple lines (such as the cout command that displays the "Choose..." prompt), you need to use backslashes at the end of each line you wish to continue into the next. Look at that command below to see how I've corrected it.
    5. Last but not least, remember those semicolons! :-p I'm not sure what it was but I think you missed the last semicolon in each block of code.


    I've fixed all the errors in your code, have a look:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    
    char input;
    int comp;
    
    cout << "Choose: \
             a. Rock \
    	 b. Paper \
    	 c. Scissors/n";
    
    cin >> input;
    
    comp = (rand() % 10) + 1;
    
    if(input = 'a')
    {
    	cout << "You choose Rock,/n";
    
    	if(comp = 1)
    	{
    	cout << "the computer choose rock./n";
    	cout << "it's a tie!";
    	}
    
    	if(comp = 2)
    	{
    	cout << "the computer choose paper./n";
    	cout << "sorry you lose.";
    	}
    
    	if(comp = 3)
    	{
    	cout << "the computer choose Scissors./n";
    	cout << "you win!.";
    	}
    }
    
    
    else if(input = 'b')
    {
    	cout << "You choose paper,/n";
    
    	if(comp = 1)
    	{
    	cout << "the computer choose rock./n";
    	cout << "you win!";
    	}
    
    	if(comp = 2)
    	{
    	cout << "the computer choose paper./n";
    	cout << "it's a tie!";
    	}
    
    	if(comp = 3)
    	{
    	cout << "the computer choose Scissors./n";
    	cout << "sorry you lose.";
    	}
    }
    
    else
    {
    	cout << "You choose scissors,/n";
    
    	if(comp = 1)
    	{
    	cout << "the computer choose rock./n";
    	cout << "sorry you lsoe.";
    	}
    
    	if(comp = 2)
    	{
    	cout << "the computer choose paper./n";
    	cout << "you win!";
    	}
    
    	if(comp = 3)
    	{
    	cout << "the computer choose Scissors./n";
    	cout << "its a tie!.";
    	}
    }
    
    }
    Although the program now compiles, it only does one game and quits and the output isn't too pretty:

    ubuntu@ubuntu:~/Desktop$ ./rps2
    Choose: a. Rock b. Paper c. Scissors/na
    You choose Rock,/nthe computer choose rock./nit's a tie!the computer choose paper./nsorry you lose.the computer choose Scissors./nyou win!.ubuntu@ubuntu:~/Desktop$ ./rps2
    Choose: a. Rock b. Paper c. Scissors/nb
    You choose Rock,/nthe computer choose rock./nit's a tie!the computer choose paper./nsorry you lose.the computer choose Scissors./nyou win!.ubuntu@ubuntu:~/Desktop$ ./rps2
    Choose: a. Rock b. Paper c. Scissors/nc
    You choose Rock,/nthe computer choose rock./nit's a tie!the computer choose paper./nsorry you lose.the computer choose Scissors./nyou win!.ubuntu@ubuntu:~/Desktop$
    However, that shouldn't be too hard to fix. Good luck!

    ---------------------------------------

    Erik, I must admit that I am completely unfamiliar with Lisp so this could be my fault but when I try to run your program with clisp I get this error:

    ubuntu@ubuntu:~/Desktop$ clisp lisp
    *** - EVAL: undefined function DEFINE
    Last edited by fatsheep; March 20, 2007 at 04:54 PM.

  4. #4
    Incinerate_IV's Avatar Burn baby burn
    Join Date
    Apr 2005
    Location
    Pennsylvania, USA
    Posts
    2,042

    Default Re: The TWC Programming Challenge

    Code:
    int main()
    {
    
    char input;
    int comp;
    
    cout << "Choose:
             a. Rock
    	 b. Paper
    	 c. Scissors/n";
    
    cin >> input;
    
    comp = (rand() % 10) + 1;
    
    if(input = a)
    {
    	cout << "You choose Rock,/n";
    
    	if(comp = 1)
    	{
    	cout << "the computer choose rock./n";
    	cout << "it's a tie!";
    	}
    
    	if(comp = 2)
    	{
    	cout << "the computer choose paper./n";
    	cout << "sorry you lose."
    	}
    
    	if(comp = 3)
    	{
    	cout << "the computer choose Scissors./n";
    	cout << "you win!."
    	}
    }
    
    
    else if(input = b)
    {
    	cout << "You choose paper,/n";
    
    	if(comp = 1)
    	{
    	cout << "the computer choose rock./n";
    	cout << "you win!";
    	}
    
    	if(comp = 2)
    	{
    	cout << "the computer choose paper./n";
    	cout << "it's a tie!"
    	}
    
    	if(comp = 3)
    	{
    	cout << "the computer choose Scissors./n";
    	cout << "sorry you lose."
    	}
    }
    
    else
    {
    	cout << "You choose scissors,/n";
    
    	if(comp = 1)
    	{
    	cout << "the computer choose rock./n";
    	cout << "sorry you lsoe.";
    	}
    
    	if(comp = 2)
    	{
    	cout << "the computer choose paper./n";
    	cout << "you win!"
    	}
    
    	if(comp = 3)
    	{
    	cout << "the computer choose Scissors./n";
    	cout << "its a tie!."
    	}
    }
    
    }
    And that's about as much as I can remember. I haven't done any programming in a while...

    I think I missed a lot of semicolons, ahh well, I hate those things.
    Last edited by Incinerate_IV; March 18, 2007 at 11:27 PM.
    THE PC Hardware Buyers Guide
    Desktop PC: Core 2 Duo E6600 @ 2.8 Ghz | Swiftech Apogee GT waterblock + MCP655 + 2 x 120mm rad | Biostar Tforce 965PT | G.Skill 4gb (2 x 2gb) DDR2-800 | Radeon HD 4870 512mb | 250GB + 160GB hard drive | Antec 900 | 22" Widescreen

  5. #5
    kshcshbash's Avatar My Good Sir CNSW
    Civitate

    Join Date
    Nov 2005
    Location
    Ontario, Canada
    Posts
    736

    Default Re: The TWC Programming Challenge

    Don't use
    Code:
    using namespace std;
    Use
    Code:
    std::
    to reference particular things.

    Also, learn SWITCH statements.
    It wouldn't hurt to do a little OOP here.
    Also,
    Code:
    int main()
    has the parameters
    Code:
    int main(int* argc, char** argv)
    It's good for to include them
    Simetrical's homeboy, yo.
    You take the blue pill and the story ends. You wake in your bed and you believe whatever you want to believe. You take the red pill and you stay in Wonderland and I show you how deep the rabbit-hole goes. Remember -- all I am offering is the truth, nothing more.

    Sign up to learn Java!

  6. #6

    Default Re: The TWC Programming Challenge

    I disagree.

    using namespace std;

    is a perfectly good statement when you know that you won't be using anything from other namespaces.

    As for OOP, this program is short enough that it is a examplary case of when not to do OOP.

    And as we are not using command line arguments, it is rather pointless to include them.

  7. #7

    Default Re: The TWC Programming Challenge

    shall I do a version of it in pascal to help newbies?

    [i][b]Working for the downfall of the Dark Tosser(s) for 5 years!
    Contact me to find out about CustomPCs for the UK!
    Like My Post? - Use the Button!
    MedievalTW - Feat. The TW Wiki
    Would you like your mod hosted for free? PM Me!

  8. #8

    Default Re: The TWC Programming Challenge

    I don't think anyone teaches pascal anymore.

  9. #9

    Default Re: The TWC Programming Challenge

    idk, learnt it years ago and can still remember making random number generators etc in it, meh, anyone done a C++ version or delphi one?, or should i do one that runs from an ISO

    EDIT: According to lusted pascal is stil taught :O
    Last edited by King Edward I; March 19, 2007 at 05:20 PM.

    [i][b]Working for the downfall of the Dark Tosser(s) for 5 years!
    Contact me to find out about CustomPCs for the UK!
    Like My Post? - Use the Button!
    MedievalTW - Feat. The TW Wiki
    Would you like your mod hosted for free? PM Me!

  10. #10
    kshcshbash's Avatar My Good Sir CNSW
    Civitate

    Join Date
    Nov 2005
    Location
    Ontario, Canada
    Posts
    736

    Default Re: The TWC Programming Challenge

    KingEdward, Pascal friggin owns!
    Even if they've never coded before, reading Pascal syntax will surely help people get the feel of the logical flow.
    Simetrical's homeboy, yo.
    You take the blue pill and the story ends. You wake in your bed and you believe whatever you want to believe. You take the red pill and you stay in Wonderland and I show you how deep the rabbit-hole goes. Remember -- all I am offering is the truth, nothing more.

    Sign up to learn Java!

  11. #11
    Lusted's Avatar Look to the stars
    Join Date
    Jan 2005
    Location
    Brighton, Sussex, England.
    Posts
    18,184

    Default Re: The TWC Programming Challenge

    Not really, i found visual basic much easier to learn. Learning about Pascal and friggin pseudo-code just got confusing when you didn't even know 1 language very well.
    Creator of:
    Lands to Conquer Gold for Medieval II: Kingdoms
    Terrae Expugnandae Gold Open Beta for RTW 1.5
    Proud ex-Moderator and ex-Administrator of TWC from Jan 06 to June 07
    Awarded the Rank of Opifex for outstanding contributions to the TW mod community.
    Awarded the Rank of Divus for oustanding work during my times as Administrator.

  12. #12
    kshcshbash's Avatar My Good Sir CNSW
    Civitate

    Join Date
    Nov 2005
    Location
    Ontario, Canada
    Posts
    736

    Default Re: The TWC Programming Challenge

    Quote Originally Posted by Lusted View Post
    Not really, i found visual basic much easier to learn. Learning about Pascal and friggin pseudo-code just got confusing when you didn't even know 1 language very well.
    Oh man. I LOVE Pascal. Whatever tickles your pickle, I suppose...
    Simetrical's homeboy, yo.
    You take the blue pill and the story ends. You wake in your bed and you believe whatever you want to believe. You take the red pill and you stay in Wonderland and I show you how deep the rabbit-hole goes. Remember -- all I am offering is the truth, nothing more.

    Sign up to learn Java!

  13. #13
    fatsheep's Avatar Civitate
    Join Date
    Aug 2004
    Location
    Somewhere
    Posts
    1,931

    Default Re: The TWC Programming Challenge

    Anyone want to take a stab at another entry? Or a revision of their first entry?

  14. #14
    kshcshbash's Avatar My Good Sir CNSW
    Civitate

    Join Date
    Nov 2005
    Location
    Ontario, Canada
    Posts
    736

    Default Re: The TWC Programming Challenge

    Sure I'll whack in a PHP entry once I can make my computer stop hating me...


    or I'll just NOT build from sources.
    Simetrical's homeboy, yo.
    You take the blue pill and the story ends. You wake in your bed and you believe whatever you want to believe. You take the red pill and you stay in Wonderland and I show you how deep the rabbit-hole goes. Remember -- all I am offering is the truth, nothing more.

    Sign up to learn Java!

  15. #15
    Erik's Avatar Dux Limitis
    Join Date
    Aug 2004
    Location
    Amsterdam
    Posts
    15,653

    Default Re: The TWC Programming Challenge

    OK, here is my entry in Lisp (Scheme)

    (define human 'Human)
    (define computer 'Computer)
    (define nobody 'Nobody)
    (define rock 'Rock)
    (define paper 'Paper)
    (define scissors 'Scissors)

    (define human-wins 0)
    (define computer-wins 0)
    (define ties 0)

    (define (computer-choose)
    (begin (random-seed (abs (current-milliseconds)))
    (case (random 3)
    ((0) rock)
    ((1) paper)
    ((2) scissors))))

    (define (human-choose)
    (begin
    (printf "Choose Rock, Paper or Scissors:~n>")
    (case (read)
    ((r R rock Rock) rock)
    ((p P paper Paper) paper)
    ((s S scissors Scissors) scissors)
    (else (begin
    (printf "Wrong input, ")
    (human-choose))))))

    (define (winner? human-choice computer-choice)
    (if (eq? human-choice computer-choice)
    nobody
    (if (or (and (eq? human-choice rock) (eq? computer-choice scissors))
    (and (eq? human-choice paper) (eq? computer-choice rock))
    (and (eq? human-choice scissors) (eq? computer-choice paper)))
    human
    computer)))

    (define (play)
    (let* ((human-choice (human-choose))
    (computer-choice (computer-choose))
    (winner (winner? human-choice computer-choice)))
    (begin
    (printf "Human: ~v, Computer: ~v -> ~v wins!~n" human-choice computer-choice winner)
    (cond
    ((eq? winner human) (set! human-wins (add1 human-wins)))
    ((eq? winner computer) (set! computer-wins (add1 computer-wins)))
    ((eq? winner nobody) (set! ties (add1 ties))))
    (printf "Score: Human ~v Computer ~v Ties ~v~n" human-wins computer-wins ties)
    (printf "Play again?~n>")
    (case (read)
    ((y Y yes Yes) (play))
    (else (printf "Good bye!~n"))))))

    (play)
    (Sorry for the (lack of) indentation, I don't know how I can prevent this forum from removing white spaces).

    Sample output:
    Welcome to DrScheme, version 360.
    Language: Textual (MzScheme, includes R5RS).
    Choose Rock, Paper or Scissors:
    >rock
    Human: Rock, Computer: Scissors -> Human wins!
    Score: Human 1 Computer 0 Ties 0
    Play again?
    >yes
    Choose Rock, Paper or Scissors:
    >Paper
    Human: Paper, Computer: Scissors -> Computer wins!
    Score: Human 1 Computer 1 Ties 0
    Play again?
    >Yes
    Choose Rock, Paper or Scissors:
    >s
    Human: Scissors, Computer: Scissors -> Nobody wins!
    Score: Human 1 Computer 1 Ties 1
    Play again?
    >no
    Good bye!
    >
    Last edited by Erik; March 20, 2007 at 01:52 PM.



  16. #16

    Default Re: The TWC Programming Challenge

    lol @ this thread. I actually learned both turbo pascal and basic in high school, and made simple games in both. for the life of me i couldn't do much of anything in either though, but thanks guys for the memories.
    Patronized by happyho in the Legion of Rahl
    Quote Originally Posted by Eugene Debs
    The Republican and Democratic parties, or, to be more exact, the Republican-Democratic party, represent the capitalist class in the class struggle. They are the political wings of the capitalist system and such differences as arise between them relate to spoils and not to principles.

  17. #17

    Default Re: The TWC Programming Challenge

    @Erik
    is it just a coincidence that the computer chose scissors every time in your sample?
    Chivalry - Total War I Settlement Plans and Buildings Dev...
    and Public Relations person, pm me with any specific questions concerning the mod...

    Consilium Belli member

    under the patronage of Sétanta

  18. #18
    Erik's Avatar Dux Limitis
    Join Date
    Aug 2004
    Location
    Amsterdam
    Posts
    15,653

    Default Re: The TWC Programming Challenge

    Quote Originally Posted by drak10687 View Post
    @Erik
    is it just a coincidence that the computer chose scissors every time in your sample?
    Yes, just a coincidence.
    There is nothing wrong with my code if that's what you were thinking.



  19. #19

    Default Re: The TWC Programming Challenge

    Too lazy to fix it. Also, I expected the input to be in capital letters.

    I do know what is wrong though, it is \n not /n

  20. #20
    chris_uk_83's Avatar Physicist
    Join Date
    Feb 2007
    Location
    Lancaster, England
    Posts
    818

    Default Re: The TWC Programming Challenge

    Here's my (rather long and convoluted) attempt at the rock paper scissors game with a java applet. I'm crap at layout so far so you'll have to excuse the poor graphics as I'm only learning the language.

    I now know how to do those scrolling boxes that everyone else has put their code in.
    Code:
    import java.util.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class RPS extends JApplet {
        private JButton rock = new JButton("rock"),
                paper = new JButton("paper"),
                scissors = new JButton("scissors");
        private JTextArea human = new JTextArea("human chose"),
                computer = new JTextArea("computer chose"),
                score = new JTextArea("winner is");
        private String humanChoice = null;
        private String compChoice = null;
        private String winner = "not defined yet";
        public void choose() {
            Random rand = new Random();
            int i = rand.nextInt(3);        
            switch(i) {
                case 0: compChoice = "rock"; break;
                case 1: compChoice =  "paper"; break;
                case 2: compChoice =  "scissors"; break;
                default: compChoice = "failed";
            }
            System.out.println(i);
            System.out.println(compChoice);
        }
        public String winner() {
            if(humanChoice == compChoice)
                return "draw";
            else if(humanChoice == "rock" && compChoice == "paper")
                return "Computer Wins";
            else if(humanChoice == "rock" && compChoice == "scissors")
                return "Human wins";
            else if(humanChoice == "paper" && compChoice == "rock")
                return "Human wins";
            else if(humanChoice == "paper" && compChoice == "scissors")
                return "Computer wins";
            else if(humanChoice == "scissors" && compChoice == "paper")
                return "Human wins";
            else if(humanChoice == "scissors" && compChoice == "rock")
                return "Computer wins";
            else
                return "failed";
        }
        public void init() {
            Container cont = getContentPane();
            cont.setLayout(new GridLayout(2, 3));
            rock.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    choose();
                    //winner();
                    computer.setText("Computer chose: \n" + compChoice);
                    humanChoice = "rock";
                    human.setText("You chose: \n" + humanChoice);
                    score.setText(winner());
                }
            });
            paper.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    choose();
                    //winner();
                    computer.setText("Computer chose: \n" + compChoice);
                    humanChoice = "paper";
                    human.setText("You chose: \n" + humanChoice);
                    score.setText(winner());
                }
            });
            scissors.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    choose();
                    //winner();
                    computer.setText("Computer chose: \n" + compChoice);
                    humanChoice = "scissors";
                    human.setText("You chose: \n" + humanChoice);
                    score.setText(winner());
                }
            });
            cont.add(human);
            cont.add(score);
            cont.add(computer);
            cont.add(rock);
            cont.add(paper);
            cont.add(scissors);
        }   
    }
    Last edited by chris_uk_83; March 30, 2007 at 02:36 AM.

Page 1 of 2 12 LastLast

Posting Permissions

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