So i have been working on a program called mastermind(the program is made so the computer picks 4 numbers and you have to guess it)
well i have been having trouble with my program and i have been having a problem with my arrays changing with each other.
for example, the computer will pick a number like 1 2 3 4 and i will guess 1 2 3 3. Well in the program to tell what has been guessed already you have to change the computer number in that array to "0" so what i did was make another array that copies the computer so that i can use it and keep the original Computer number.
But for some odd reason the computer number will still change even though i am not telling it to change.
TL;DR
i make an array of random number and then make a copy of it to change, when it changes, it changes the original
and i want the copy to keep resetting after each time.
/** * @(#)Mastermind.java * Makes a game where you guess a number 1-4 and compares it to the computers number * */ import java.util.Scanner; import java.io.*; import java.util.ArrayList; import java.util.Random; public class Mastermind { /** * Creates a new instance of <code>Mastermind</code>. */ public Mastermind() { } public static int bull = 0; public static int cow = 0; public static int pTry = 0; public static ArrayList<Integer> test = new ArrayList<Integer>(){{ add(0); add(0); add(0); add(0); }}; public static ArrayList<Integer> comp = new ArrayList<Integer>(){{ add(0); add(0); add(0); add(0); }}; public static ArrayList<Integer> player = new ArrayList<Integer>(){{ add(0); add(0); add(0); add(0); }}; public static ArrayList<Integer> comp2 = new ArrayList<Integer>(){{ add(0); add(0); add(0); add(0); }}; //Main public static void main(String[] args) { CNumber(); while(bull != 4) { System.out.println("Computer Number" + comp); PNumber(); System.out.println("You have chosen: " + player); Bull(); Cow(); Tries(); } System.out.print ("\n\nYou Won!\nCongratulations!!!\n\n\n\n\n\n"); } //generates the computer number public static void CNumber() { Random rand = new Random(); for(int p = 0; p < 4; p++) { comp.set(p, rand.nextInt(4)+ 1); } } //the player number makes his input public static void PNumber() { Scanner scan = new Scanner(System.in); System.out.print ("Enter 4 numbers (1-4) hitting enter after each number:\n"); for(int i = 0; i < 4; i++) { player.set(i,scan.nextInt()); } } //computes the number of bulls public static void Bull() { ArrayList<Integer> comp2 = new ArrayList<Integer>(comp); bull = 0; comp2 = comp; for(int i = 0; i < comp2.size(); i++) { int x = comp2.get(i); int y = player.get(i); if(x == y) { bull++; comp2.set(i, 0); player.set(i, 99); } } System.out.println("You have " + bull + " bulls"); } //Tells you how many cows you have public static void Cow( ) { for(int i = 0; i < comp2.size(); i++) { int woc = comp2.get(i); for(int j = 0; j < comp2.size(); j++) { int kow = player.get(j); if(woc==kow) { cow++; comp2.set(j, 0); player.set(i, 99); } } } System.out.println("You have " + cow + " cows"); } //Tells you how many tires you have had public static void Tries() { pTry ++; System.out.println("You have had " + pTry + " tries"); } }
Thanks in advance!