簡體   English   中英

使用哈希圖的RockPaperScissors游戲

[英]RockPaperScissors game using hashmap

我工作的一個剪刀石頭布的游戲我的編程類和教授想讓我們用一個hash map來存儲用戶的模式和times模式發生在hash map

因此,我創建了一個Pattern類,該類包含一個值數組,並在Computer類中將其存儲在hash map 我打算讓該程序工作的方式是該程序將首先基於hash mappatterns generate a move 如果地圖為空,則只會生成隨機移動。 然后,在用戶進行移動之后,他的移動將被放入一個數組中以創建一個新的模式,並且該模式將被保存到哈希圖中。 如果模式已經在地圖內,則發生的次數將會增加。

通過將用戶的最后三個動作與地圖中的模式進行比較來做出預測的動作,並查看用戶接下來可能拋出的動作。 因此,如果用戶最近四步移動是: RPSR則程序將采用PSR ,然后添加R P S並查看這些模式是否在地圖中。 如果是這樣,它將看到最有可能發生的事件。 然后,如果用戶接下來播放R ,則該陣列將更新為PSRR ,並且模式將繼續。

因此,初學者模式是從空地圖開始,而退伍軍人模式是加載以前保存的地圖。 但是,我遇到了一些問題:

  1. put模式和時間put入哈希圖中之后,當我嘗試對其進行遍歷並查看其在映射中存儲的模式時,我發現所有模式都是相同的,並且這種情況不會發生。 該模式假定為: R -> RP - > RPS (如果用戶分別扔石頭,紙,剪刀),但現在只顯示RPS -> RPS -> RPS 這可以在Computer的getSize()中看到。
  2. 第四NullPointerException之后,我遇到了NullPointerException 如果我可以解決上一個問題,則可能會解決該問題,但是我不知道為什么會發生。
  3. 當我嘗試從文件中讀取地圖時收到警告,所以我只是想知道警告是否可能使程序混亂。
    Unchecked cast from Object to HashMap<Pattern, Integer>

我的程序出現問題的一些幫助或指針將不勝感激。

電腦:

import java.io.*;
import java.util.*;

public class Computer {
    /**
     * The hashmap that will holds the pattern and how many times it occured.
     */
    private HashMap<Pattern, Integer> map;

    /**
     * Constructor
     */
    public Computer() {
        map = new HashMap<Pattern, Integer>();
    }

    /**
     * Storing the pattern to the map.
     * 
     * @param p
     *            The pattern that will be saved to the map.
     */
    public void storePattern(Pattern p) {
        Integer time = map.get(p);
        // If time is null then the Pattern is not yet in the hashmap
        if (time == null) {
            map.put(p, 1);
        } else {
            map.put(p, time + 1);
        }
    }

    /**
     * Generating the computer's next move.
     * 
     * @return The move that the computer will make.
     */
    public char generateMove(Pattern user) {
        int r = 0, p = 0, s = 0;
        char returns = 'a';
        if (!map.isEmpty()) {
            char[] userPatts = user.getPattern();
            char[] patts = userPatts.clone();
            patts[patts.length - 1] = 'R';
            Pattern testPatt = new Pattern(patts);
            if (map.containsKey(testPatt))
                r = map.get(patts);
            patts[patts.length - 1] = 'P';
            testPatt = new Pattern(patts);
            if (map.containsKey(testPatt))
                p = map.get(patts);
            patts[patts.length - 1] = 'S';
            testPatt = new Pattern(patts);
            if (map.containsKey(testPatt))
                s = map.get(patts);
            if ((s - r) > 0 && (s - p) > 0)
                return 'R';
            if ((p - s) > 0 && (p - r) > 0)
                return 'S';
            if ((r - s) > 0 && (r - p) > 0)
                return 'P';
            if (s == r && r != 0)
                return 'P';
            if (s == p && s != 0)
                return 'R';
            if (r == p && p != 0)
                return 'S';
        }
        // Throwing a random move
        int max = (int) (Math.random() * 3) + 1;
        if (max == 1)
            returns = 'P';
        else if (max == 2)
            returns = 'S';
        else if (max == 3)
            returns = 'R';
        return returns;
    }

    /**
     * Loading the hashmap from a file.
     */
    public void loadMap() {
        File f = new File("HashMap.dat");
        if (f.exists()) {
            try {
                ObjectInputStream in = new ObjectInputStream(
                        new FileInputStream(f));
                map = (HashMap<Pattern, Integer>) in.readObject();
                System.out.println("Successfully loaded.");
                in.close();
            } catch (IOException e) {
                System.out.println("Error processing file.");
            } catch (ClassNotFoundException e) {
                System.out.println("Could not find class.");
            }
        }
    }

    /**
     * Saving the hashmap to a file.
     */
    public void saveMap() {
        File f = new File("HashMap.dat");
        try {
            ObjectOutputStream out = new ObjectOutputStream(
                    new FileOutputStream(f));
            out.writeObject(map);
            System.out.println("Map saved.");
            out.close();
        } catch (IOException e) {
            System.out.println("Error processing file.");
        }
    }

    public void getSize() {
        System.out.println("Map size: " + map.size());
        for (Map.Entry<Pattern, Integer> entry : map.entrySet()) {
            Pattern b = entry.getKey();
            char[] a = b.getPattern();
            for (int i = 0; i < a.length; i++) {// Why a.length allows i to go
                                                // from 0 to 3 if a.length == 4?
                System.out.print(a[i] + " ");// Why are all the patterns the
                                                // same?
            }
            System.out.println();
        }
    }
}

圖案:

import java.io.Serializable;
import java.util.Arrays;

public class Pattern implements Serializable {
    /**
     * Array that holds the patterns.
     */
    private char[] pattern;

    /**
     * Constructor.
     */
    public Pattern(char[] patt) {
        pattern = patt;
    }

    /**
     * Getting the pattern array.
     * 
     * @return The pattern array.
     */
    public char[] getPattern() {
        return pattern;
    }

    /**
     * Override the hashCode().
     */
    @Override
    public int hashCode() {
        return Arrays.hashCode(pattern);
    }

    /**
     * Override the equals()
     */
    @Override
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        }
        if (!(o instanceof Pattern)) {
            return false;
        }
        Pattern s = (Pattern) o;
        return Arrays.equals(s.getPattern(), pattern);
    }
}

主要:

import java.util.Scanner;

/**
 * This program allows the user to play Rock Paper Scisors with a computer with
 * a twist: The computer will try to predict the user's next move and try to
 * beat it.
 * 
 * @author:
 */
public class RockPaperScisors {
    public static void main(String[] args) {
        char computer = 'S';
        int playerScore = 0, compScore = 0, tie = 0, full = 0;
        char[] patt = new char[4];
        Computer comp = new Computer();
        boolean stop = false;
        System.out
                .println("Do you want to play veteran or beginner mode?\n1. Veteran\n2. Beginner");
        int mode = input(2, 1);
        if (mode == 1)
            comp.loadMap();
        comp.getSize();
        while (!stop) {
            // Generate computer's move.
            computer = comp.generateMove(new Pattern(patt));
            System.out.println("Enter R P S. Enter Q to quit.");
            char a = input();
            if (a == 'Q') {
                stop = true;
                break;
            }
            System.out.println("You threw: " + a);
            if (full <= (patt.length - 1)) {
                patt[full] = a;
                full++;
            } else {
                for (int i = 0; i <= patt.length - 2; i++) {
                    patt[i] = patt[i + 1];
                }
                patt[patt.length - 1] = a;
            }
            for (int i = 0; i <= patt.length - 1; i++) {
                System.out.print(patt[i]);
            }
            System.out.println();
            // Store the new pattern
            comp.storePattern(new Pattern(patt));
            System.out.println("Computer plays: " + computer);
            // Check for win or tie
            if (a == computer) {
                System.out.println("Tie.");
                tie++;
            } else {
                if (a == 'R' && computer == 'P') {
                    System.out.println("Computer wins.");
                    compScore++;
                }
                if (a == 'R' && computer == 'S') {
                    System.out.println("Player wins.");
                    playerScore++;
                }
                if (a == 'P' && computer == 'S') {
                    System.out.println("Computer wins.");
                    compScore++;
                }
                if (a == 'P' && computer == 'R') {
                    System.out.println("Player wins.");
                    playerScore++;
                }
                if (a == 'S' && computer == 'R') {
                    System.out.println("Computer wins.");
                    compScore++;
                }
                if (a == 'S' && computer == 'P') {
                    System.out.println("Player wins.");
                    playerScore++;
                }
            }
            // Saving the map
            comp.saveMap();
            comp.getSize();
            System.out.println("Your score: " + playerScore + "\tTie: " + tie
                    + "\tComputer score: " + compScore);
        }
        System.out.println("Thank you for playing.");
    }

    public static int input(int upper, int lower) {
        Scanner in = new Scanner(System.in);
        boolean valid = false;
        int validInt = 0;
        while (!valid) {
            if (in.hasNextInt()) {
                validInt = in.nextInt();
                if (validInt <= upper && validInt >= lower) {
                    valid = true;
                } else {
                    System.out.print("Invalid- Retry: ");
                }
            } else {
                in.next();
                System.out.print("Invalid input- Retry: ");
            }
        }
        return validInt;
    }

    public static char input() {
        Scanner in = new Scanner(System.in);
        boolean valid = false;
        char validChar = 'a';
        while (!valid) {
            if (in.hasNext()) {
                validChar = in.next().charAt(0);
                if (validChar == 'R' || validChar == 'P' || validChar == 'S'
                        || validChar == 'Q') {
                    valid = true;
                } else {
                    System.out.print("Invalid- Retry: ");
                }
            } else {
                in.next();
                System.out.print("Invalid input- Retry: ");
            }
        }
        return validChar;
    }
}

如果a.length == 4,為什么a.length允許我從0變為3?

在計算機科學中,您從0開始計數,因此長度為4

int array = new array[4];
array[0];
array[1];
array[2];
array[3];

為什么所有模式都一樣?

while (!stop)主要內部while (!stop)應該嘗試patt = new char[4]; 確保您不會一遍又一遍地使用相同的引用,因為更改基礎對象也會更改所有引用。

只是為了闡明引用的含義: Java是“按引用傳遞”還是“按值傳遞”?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM