-
Notifications
You must be signed in to change notification settings - Fork 0
/
miniMaxReaching20.java
51 lines (51 loc) · 2.36 KB
/
miniMaxReaching20.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.util.*;
class miniMaxReaching20{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int choice=0; // two cases 1,2
int sum=0; // choice + previoussum
int playerTurns=0, computerTurns=0; //playerTurns and computerTurns
while(sum<20){
if((playerTurns+computerTurns)%2==0){ //playerTurn + computerTurn if even its players turn else computer turn.
System.out.println("It's player's choice");
System.out.println("You have the following choices to make");
while(choice!=1 || choice!=2){
System.out.println("1. make the sum to "+ (sum+1));
System.out.println("2. make the sum to "+ (sum+2));
choice=sc.nextInt(); //choice
if(choice==1){
sum=addOne(sum);//adds one to the sum
break;
}
else if(choice==2){
sum= addTwo(sum); //adds two to the sum
break;
}
else{
System.out.println("Your choice is incorrect, Please choose again!");// if not selected 1 or 2 options
}
}
System.out.println("Now the sum is "+ sum); //sums will be printed
playerTurns++; //playTurns will be increased
}
else{
System.out.println("It's computer's choice");
sum=computerChoice(sum);
System.out.println("Now the sum is "+ sum); //sums will be printed
computerTurns++; //computerTurns will be increased
}
}
System.out.println("players took "+(playerTurns+computerTurns)+" turns"); // prints how many turns are taken by both player.
sc.close();
}
public static int computerChoice(int sum){ //gives the optimized option for the computer to choose using alphabeta
Alphabeta ab= new Alphabeta(); //object of Alphabeta
return ab.run(sum);
}
public static int addOne(int result){ //returns one the result
return (result+1);
}
public static int addTwo(int result){ //returns two the result
return (result+2);
}
}