Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This assignment deals with the implementation of the game Connect Four. To reduc

ID: 3532332 • Letter: T

Question

This assignment deals with the implementation of the game Connect Four. To reduce the problem size, the game consists of agrid : 3 rows x 3 columns (or 9 cells).

There are two players (indexed by 1 and 2) who take turn to drop one token (identified by the user index) in either column 1,2 or 3, with the objective to align 3 of his/her tokens in a row, in a column or in a diagonal. In the following two examples :



Implement your flowchart with one input to get the column index where the player will drop the

token. Your flowchart must check that the selected column is not full; as long as the player keeps

selecting a column that is full, the flowchart must ask the same player for another input. After

getting a valid column index, have your flowchart update the state of the grid, display its content

and determine if the game is over or not. If the game is not over, then it is the turn of the other

player. And so on.


Here is a sample of what solution produces:

Player 1 selects column 1

Game after Player 1 move

0 0 0

0 0 0

1 0 0

Player 2 selects column 3

Game after Player 2 move

0 0 0

0 0 0

1 0 2

Player 1 selects column 1

Game after Player 1 move

0 0 0

1 0 0

1 0 2

Player 2 selects column 1

Game after Player 2 move

2 0 0

1 0 0

1 0 2

Player 1 selects column 1

The column 1 that you chose is full.

Player 1 selects column 2

Game after Player 1 move

2 0 0

1 0 0

1 1 2

Player 2 selects column 2

Game after Player 2 move

2 0 0

1 2 0

1 1 2

Player 2 wins the game


Can someone solve this problem for me to generate a flow chart?

Explanation / Answer

package tic1;


import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;


public class Tic1{

public Object[][] mat;

public List<Tic1> child;

public int win;

public int hash;

public int bash;

public int ash;

public Tic1(){

this.mat=new Object[3][3];

for(int i=0;i<3;i++){

for(int j=0;j<3;j++){

this.mat[i][j]=3*i+j;

}

}

this.child=new ArrayList<>();

this.win=0;

this.hash=0;

}

//Check whether win or loss

public void make(){

Object[][] c=this.mat;

int d=0;

int t=0;

for(int i=0;i<3;i++){

for(int j=0;j<3;j++){

if(c[i][j]=="X"){

d=d+1;

}

if(c[i][j]=="O"){

t=t+1;

}

}

if(d==3){

this.win=1;

}

else if(t==3){

this.win=-1;

}

else{

d=0;

t=0;

}

}

if(this.win!=1 && this.win!=-1){

for(int i=0;i<3;i++){

for(int j=0;j<3;j++){

if(c[j][i]=="X"){

d=d+1;

}

else if(c[j][i]=="O"){

t=t+1;

}

}

if(d==3){

this.win=1;

}

else if(t==3){

this.win=-1;

}

else{

d=0;

t=0;

}

}

}

if(this.win!=1 || this.win!=-1){

int r=0;

int y=0;

int z=0;

int x=0;

for(int i=0;i<3;i++){

if(c[i][i]=="X"){

r=r+1;

}

if(c[i][i]=="O"){

y=y+1;

}

if(c[i][2-i]=="X"){

z=z+1;

}

if(c[i][2-i]=="O"){

x=x+1;

}

}

if(r==3 || z==3){

this.win=1;

}

else if(y==3 || x==3){

this.win=-1;

}

}

}

//Creates hash function

public void create(){

Object[][] z=this.mat;

for(int i=0;i<3;i++){

for(int j=0;j<3;j++){

if(z[i][j]=="X"){

this.hash=this.hash+(3*i+(j))*11;

this.bash=this.bash-53;

}

else if(z[i][j]=="O"){

this.hash=this.hash+(3*i+(j))*13;

this.bash=this.bash+41;

}

else{

this.hash=this.hash+(3*i+(j))*(3*i+(j));

this.bash=this.hash+13*i;

}

}

}

}

public boolean full(){

int t=0;

for(int i=0;i<3;i++){

for(int j=0;j<3;j++){

if(this.mat[i][j]=="X" || this.mat[i][j]=="O"){

t=t+1;

}

}

}

if(t==9){

this.win=2;

return true;

}

else{

return false;

}

}

public void algo(){

if(this.win==1 || this.win==-1 || this.full()){

}

else{

Object[][] s=this.mat;

for(int i=0;i<3;i++){

for(int j=0;j<3;j++){

if(this.mat[i][j]!="X" && this.mat[i][j]!="O"){

Tic1 t=new Tic1();

for(int g=0;g<3;g++){

System.arraycopy(this.mat[g], 0, t.mat[g], 0, 3);

}

//System.out.println("t.print1=");

//t.print();

t.mat[i][j]="X";

//System.out.println("t.print2=");

//t.print();

t.make();

this.child.add(t);

}

}

}

for(int i=0;i<this.child.size();i++){

this.child.get(i).algo1();

}

}

}

public void algo1(){

if(this.win==1 || this.win==-1 || this.full()){

}

else{

for(int i=0;i<3;i++){

for(int j=0;j<3;j++){

if(this.mat[i][j]!="X" && this.mat[i][j]!="O"){

Tic1 t=new Tic1();

for(int e=0;e<3;e++){

System.arraycopy(this.mat[e], 0, t.mat[e], 0, 3);

}

t.mat[i][j]="O";

t.make();

this.child.add(t);

}

}

}

for(int i=0;i<this.child.size();i++){

this.child.get(i).algo();

}

}

}

public void print(){

for(int i=0;i<3;i++){

for(int j=0;j<3;j++){

System.out.print(this.mat[i][j]+" ");

}

System.out.println();

}

}

public int dbz(){

if(this.child.isEmpty()){

if(this.win==1) {

return 1;

}

else{

return 0;

}

}

else{

for(int i=0;i<this.child.size();i++){

this.ash=this.ash+this.child.get(i).dbz();

}

}

return this.ash;

}

public void rule(){

if(this.win==1){

System.out.println("You lost");

}

else if(this.win==-1){

System.out.println("You win");

}

else if(this.full()){

System.out.println("Draw");

}

else{

System.out.println("Computers turn");

Tic1 a=new Tic1();

for(int i=0;i<3;i++){

System.arraycopy(this.mat[i], 0, a.mat[i], 0, 3);

}

a.algo();

int b=-100000;

int c=-2;

for(int i=0;i<a.child.size();i++){

a.child.get(i).dbz();

if(a.child.get(i).ash>b){

b=a.child.get(i).ash;

c=i;

}

}

for(int i=0;i<3;i++){

System.arraycopy(a.child.get(c).mat[i], 0, this.mat[i], 0, 3);

}

this.print();

this.make();

this.game();

}

}

public void game(){

if(this.win==1){

System.out.println("You lost");

}

else if(this.win==-1){

System.out.println("You win");

}

else if(this.full()){

System.out.println("Draw");

}

else{

System.out.println("Enter the number");

Scanner s=new Scanner(System.in);

int a=s.nextInt();

if(this.mat[a/3][a%3]!="X"){

this.mat[a/3][a%3]="O";

this.print();

this.make();

this.rule();

}

else{

System.out.println("Wrong Entry Try Again");

game();

}

}

}

public static void main(String[] args) {

Tic1 t=new Tic1();

Object[][] x=t.mat;

for(int i=0;i<3;i++){

for(int j=0;j<3;j++){

x[i][j]=3*(i)+(j);

}

}

t.print();

t.game();

// x[0][0]="O";

//t.algo();

}

}