You’ve been asked to implement a pseudo-intelligent software “agent” (referred t
ID: 3677550 • Letter: Y
Question
You’ve been asked to implement a pseudo-intelligent software “agent” (referred to here as the ‘Agent’) that plays a simple board game we’ll call Quatre. The Quatre board is a basic grid and two players take turns placing their “stones” in open spaces with the goal of getting 4 stones in a row either horizontally or vertically. The first player to do so wins the game.
The program requires only a simple text-based depiction of the game board such as the layouts shown below. You may use the methods provided in the starter code or develop your own. After each play the board should be updated (or redrawn) to show the placement of all stones. Each player will need to have a single unique character to identify all their stones
No player should be allowed to place a stone outside the board boundaries or on an already occupied space. Aligning 4 stones in a diagonal direction does not constitute a win There are various intelligent strategies your Agent could adopt, but in its turn it should as a minimum follow these 2 priorities (in this order): 1. If possible, extend any sequence of 3 Agent stones in a row currently on the board to 4. 2. Block any sequence of 3 stones in a row that the opponent may have.
There are various intelligent strategies your Agent could adopt, but in its turn it should as a minimum follow these 2 priorities (in this order): 1. If possible, extend any sequence of 3 Agent stones in a row currently on the board to 4. 2. Block any sequence of 3 stones in a row that the opponent may have
Clearly your Agent should recognize a ‘Draw’ game when there are no more valid spaces on the board to play in. You do not need to detect a draw game before this point even if it’s impossible for either player to win given the remaining open spaces Your program requires only a single class as in the starter code (though you may choose to use more).
Create a version of the game, activated by User’s choice, that also counts 4 stones in a diagonal row as a win
Create a version of the game, activated by User’s choice, that “looks ahead” in some manner to declare a game a DRAW when it is provably not possible for either player to win. Points awarded based on level of “look ahead” implemented. Be sure to clearly describe your look-ahead methodology in comments.
Explanation / Answer
package com.question.chegg._1;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class AgentGame {
private static char[][] board = new char[4][4];
public static List<Coordinate> player = new ArrayList<Coordinate>();
public static List<Coordinate> agent = new ArrayList<Coordinate>();
public static char players = '#';
public static char agents = '#';
public static List<List<Coordinate>> winningCombinations = new ArrayList<List<Coordinate>>();
static {
List<Coordinate> w1 = new ArrayList<Coordinate>();
w1.add(new Coordinate(0, 0));
w1.add(new Coordinate(0, 1));
w1.add(new Coordinate(0, 2));
w1.add(new Coordinate(0, 3));
List<Coordinate> w2 = new ArrayList<Coordinate>();
w2.add(new Coordinate(0, 0));
w2.add(new Coordinate(1, 0));
w2.add(new Coordinate(2, 0));
w2.add(new Coordinate(3, 0));
List<Coordinate> w3 = new ArrayList<Coordinate>();
w3.add(new Coordinate(0, 1));
w3.add(new Coordinate(1, 1));
w3.add(new Coordinate(2, 1));
w3.add(new Coordinate(3, 1));
List<Coordinate> w4 = new ArrayList<Coordinate>();
w4.add(new Coordinate(0, 2));
w4.add(new Coordinate(1, 2));
w4.add(new Coordinate(2, 2));
w4.add(new Coordinate(3, 2));
List<Coordinate> w5 = new ArrayList<Coordinate>();
w5.add(new Coordinate(0, 3));
w5.add(new Coordinate(1, 3));
w5.add(new Coordinate(2, 3));
w5.add(new Coordinate(3, 3));
List<Coordinate> w6 = new ArrayList<Coordinate>();
w6.add(new Coordinate(1, 0));
w6.add(new Coordinate(1, 1));
w6.add(new Coordinate(1, 2));
w6.add(new Coordinate(1, 3));
List<Coordinate> w7 = new ArrayList<Coordinate>();
w7.add(new Coordinate(2, 0));
w7.add(new Coordinate(2, 1));
w7.add(new Coordinate(2, 2));
w7.add(new Coordinate(2, 3));
List<Coordinate> w8 = new ArrayList<Coordinate>();
w8.add(new Coordinate(3, 0));
w8.add(new Coordinate(3, 1));
w8.add(new Coordinate(3, 2));
w8.add(new Coordinate(3, 3));
List<Coordinate> w9 = new ArrayList<Coordinate>();
w9.add(new Coordinate(0, 0));
w9.add(new Coordinate(1, 1));
w9.add(new Coordinate(2, 2));
w9.add(new Coordinate(3, 3));
List<Coordinate> w10 = new ArrayList<Coordinate>();
w10.add(new Coordinate(0, 3));
w10.add(new Coordinate(1, 2));
w10.add(new Coordinate(2, 2));
w10.add(new Coordinate(3, 0));
winningCombinations.add(w10);
winningCombinations.add(w1);
winningCombinations.add(w2);
winningCombinations.add(w3);
winningCombinations.add(w4);
winningCombinations.add(w5);
winningCombinations.add(w6);
winningCombinations.add(w7);
winningCombinations.add(w8);
winningCombinations.add(w9);
}
public static void main(String[] args) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
board[i][j] = '-';
System.out.print(board[i][j] + " ");
}
System.out.println();
}
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("Please enter your coordinates to walk : X[0->3] Y[0->3] Your sign #");
int x = sc.nextInt();
int y = sc.nextInt();
Coordinate temp = new Coordinate(x, y);
if (!(0 <= x && x <= 3) || !(0 <= y && y <= 3) || !checkValidChance(temp)) {
System.out.println("invalid input please retry.");
} else {
player.add(temp);
board[x][y] = '#';
if (player.size() == 3) {
if (winning() != null) {
System.out.println(winning() + " Won.");
break;
}
Coordinate temp2 = isPlayerWinning();
if (temp2 != null) {
agent.add(temp2);
board[temp2.getX()][temp2.getY()] = '*';
} else {
temp2 = isPlayerWinning();
if(temp2 == null){
temp2 = getAgentCoordinate();
}
if (temp2 == null) {
System.out.println("DRAW!!!");
break;
} else {
agent.add(temp2);
board[temp2.getX()][temp2.getY()] = '*';
}
}
} else {
if (winning() != null) {
System.out.println(winning() + " Won.");
break;
}
Coordinate temp2 = getAgentCoordinate();
if (temp2 == null) {
System.out.println("DRAW!!!");
break;
} else {
agent.add(temp2);
board[temp2.getX()][temp2.getY()] = '*';
}
}
}
display();
if (winning() != null) {
System.out.println(winning() + " Won.");
break;
}
}
display();
}
public static String winning() {
for (List<Coordinate> co : winningCombinations) {
List<Coordinate> list = new ArrayList<Coordinate>(co);
list.removeAll(player);
if (list.size() == 0) {
System.out.println(players);
System.out.println(co);
return "Player";
}
list = new ArrayList<Coordinate>(co);
list.removeAll(agent);
if (list.size() == 0) {
System.out.println(agents);
System.out.println(co);
return "Agent";
}
}
return null;
}
private static Coordinate getAgentCoordinate() {
for (List<Coordinate> co : winningCombinations) {
boolean b = true;
for (Coordinate coo : player) {
if (co.contains(coo)) {
b = false;
break;
}
}
if (b) {
List<Coordinate> list = new ArrayList<Coordinate>(co);
for (Coordinate coo : agent) {
list.remove(coo);
}
if (list.size() > 0) {
return list.get(0);
}
}
}
return null;
}
public static void display() {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
private static Coordinate isPlayerWinning() {
for (List<Coordinate> co : winningCombinations) {
List<Coordinate> temp = new ArrayList<Coordinate>(co);
temp.removeAll(player);
if (temp.size() == 1) {
return temp.get(0);
}
}
return null;
}
private static boolean checkValidChance(Coordinate temp) {
if (player.indexOf(temp) != -1 || agent.indexOf(temp) != -1) {
return false;
}
return true;
}
static class Coordinate {
int x;
int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Coordinate(int x, int y) {
super();
this.x = x;
this.y = y;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public String toString() {
return "[x=" + x + ", y=" + y + "]";
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Coordinate other = (Coordinate) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.