vendredi 23 octobre 2015

Wrong outputting if player won or lost at coin tossing

I am trying to get the right output if the player has won or lost in my coin tossing game. The game flips a virtual coin 100 times and if the player has selected the coin that has the most flips wins and if less then they have selected will lose. But the output always comes out wrong.

public class Strategy extends Object {

        /**
         * 
         * Encoding for a strategy.
         */

        int opponentLastMove = 1;

        int myLastMove = 1;

        String name;

        // 0 = defect, 1 = cooperate

        public Strategy()

        {

        } /* Strategy */

        public int nextMove()

        {

            return 0;

        } /* nextMove */

        public void saveOpponentMove(int move) {
            opponentLastMove = move;
        }

        public int getOpponentLastMove() {
            return opponentLastMove;
        }

        public void saveMyMove(int move) {
            myLastMove = move;
        }

        public int getMyLastMove() {
            return myLastMove;
        }

        public String getName() {
            return name;
        }

    }

public class StrategyRandom extends Strategy

{

    /**
     * 
     * Encoding for a strategy.
     */

    // 0 = defect, 1 = cooperate

    public StrategyRandom()

    {

        name = "Random";

    } /* StrategyRandom */

    public int nextMove()

    {

        if (Math.random() < 0.5)
            return 1;

        return 0;

    } /* nextMove */

}

import java.util.Scanner;

public class GameDriver {

    public static void main(String[] args) {

        try {

            StrategyRandom stratRandom = new StrategyRandom();

            Scanner scanner = new Scanner(System.in);
            int choice = 1;
            int heads;
            int tails;
            int coinFlip = 0;

            System.out.print("Enter your name: ");
            stratRandom.name = scanner.nextLine();

            System.out.println("Press 1 for Heads" + "\nPress 2 for Tails"
                    + "\nPress 0 to Exit");
            choice = Integer.parseInt(scanner.nextLine());

            while (choice != 0) {

                heads = 0;
                tails = 0;

                for (int i = 0; i < 100; i++) {
                    coinFlip = stratRandom.nextMove();

                    if (coinFlip == 0) {
                        heads++;
                        System.out.print("H,");

                    } else {
                        tails++;
                        System.out.print("T,");

                    }

                }

                if (coinFlip == 1) {

                    System.out.println("\nYou Won");

                } else {

                    System.out.print("\nYou Lost");
                }

                System.out.println("\nTimes head was flipped: " + heads);
                System.out.println("Times tails was flipped: " + tails);
                System.out.print("\nHello " + stratRandom.getName()
                        + ", Enter a number or 0 to exit: ");
                choice = Integer.parseInt(scanner.nextLine());
            }

        } catch (Exception ex) {

            ex.printStackTrace();

        }

    }
}

Aucun commentaire:

Enregistrer un commentaire