'How can I add a reset and restart to this when a button is pressed after the game is over?
I am trying to write this code in processing. A Connect Four game uses an Arduino as a controller for each row.
After a winner is detected, the game stops. But I want to make the game reset the table and restart after any button is pushed, and the problem is I am not sure how to do that.
Thanks in advance!
Here's the code:
//Connect Four Game
//Mohammed Sharifpour
//2nd of February, 2022
import processing.serial.*;
Serial ADP;
int squareSize = 64;
String serialName = "COM6"; //Can be modified to the indicated COM Port
color pc[] = {color(255, 75,0), color(48, 213, 200)};
String PCN[] = {"Orange", "Turqoise"};
int playerTurn = 0;
int grid[][] = new int[7][8];
boolean finished = false;
color backgroundColor = color(0, 0, 0);
color lineColor = color(255, 255, 255);
void Circle(int x, int y, color c){
fill(c);
noStroke();
circle(x * squareSize + 0.5 * squareSize, y * squareSize + 0.5 * squareSize, squareSize);
}
void draw(){
if (ADP.available() > 0) keypressed(ADP.read()-1);
}
void printText(String message){
fill(backgroundColor);
noStroke();
rect(0, 6 * squareSize + 1, 7 * squareSize + 1, (6 + 1) * squareSize + 1);
fill(lineColor);
text(message, 16, 6 * squareSize + squareSize * 0.75);
}
void setup(){
size(480, 480);
noCursor();
background(backgroundColor);
stroke(lineColor);
for(int x = 0; x <= 7 * squareSize; x += squareSize){
line(x, 0, x, 6 * squareSize);
}
for(int y = 0; y <= 6 * squareSize; y += squareSize){
line(0, y, 7 * squareSize, y);
}
//RGB Code for Circle "Orange" (255, 75,0);
//RGB Code for Circle "Turqoise" (48, 213, 200);
printArray(Serial.list());
ADP = new Serial(this, serialName, 9600);
textSize(squareSize / 4);
printText("Welcome to Connect Four, press any button to start!");
}
boolean handleDrop(int column){
boolean dropped = false;
for(int y = 6 - 1; y >= 0; y--){
if(grid[column][y] == 0){
grid[column][y] = playerTurn;
Circle(column, y, pc[playerTurn - 1]);
dropped = true;
break;
}
}
return dropped;
}
void keypressed(int bs){
if(bs < 2 || bs > 7 + 1) return;
if(finished) return;
boolean dropped = false;
if(playerTurn > 0){
dropped = handleDrop(bs - 2);
if(!dropped) return;
}
playerTurn++;
if(playerTurn > 2) playerTurn = 1;
int winner = testWinner();
if(winner > 0){
finished = true;
textSize(squareSize / 3);
printText(PCN[winner - 1] + " won!, GAME OVER");
return;
}
printText(PCN[playerTurn - 1] + "'s turn");
}
int testFour(int startX, int startY, int incX, int incY){
int box = -1;
for(int x = startX, y = startY, i = 0; i < 4; x += incX, y += incY, i++){
if(box < 0){
box = grid[x][y];
}else{
if(grid[x][y] != box) return 0;
}
}
if(box > 0){
int startXPixel = startX * squareSize + squareSize / 2;
int startYPixel = startY * squareSize + squareSize / 2;
stroke(lineColor);
strokeWeight(4);
line(startXPixel, startYPixel, startXPixel + 3 * incX * squareSize, startYPixel + 3 * incY * squareSize);
}
return box;
}
int testWinner(){
int winner = 0;
for(int x = 0; x < 7 - 3; x++){
for(int y = 0; y < 6 ; y++){
winner = testFour(x, y, 1, 0);
if(winner > 0) return winner;
}
}
for(int y = 0; y <6 - 3; y++){
for(int x = 0; x < 7; x++){
winner = testFour(x, y, 0, 1);
if(winner > 0) return winner;
}
}
for(int x = 0; x < 7 - 3; x++){
for(int y = 0; y < 6 - 3; y++){
winner = testFour(x, y, 1, 1);
if(winner > 0) return winner;
}
}
for(int x = 0; x < 7 - 3; x++){
for(int y = 6 - 1; y >= 3; y--){
winner = testFour(x, y, 1, -1);
if(winner > 0) return winner;
}
}
return winner;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
