This time I played with gravity four-in-a-row on the console! It works like this.
Since I made it for fun, there may be omissions in the judgment part, but in the future I will make improvements such as adding CPU battles based on this. About four-in-a-row with gravity
wiki https://ja.wikipedia.org/wiki/%E5%9B%9B%E7%9B%AE%E4%B8%A6%E3%81%B9
・ Windows 7 · Java 8 ・ Eclipse
I will introduce the source of this time.
ConnectFour.java
package connect.main;
import java.util.Random;
import java.util.Scanner;
public class ConnectFour {
public static void main(String[] args) {
System.out.println("Start four-in-a-row with gravity");
// 0:player1 1:player2
int player = 0;
Random random = new Random();
int turn = 0;
int[][] bord = new int[6][7];
int i;
//Board initialization(-1)
for(i = 0; i < 6; i++) {
for(int j = 0; j < 7; j++) {
bord[i][j] = -1;
}
}
// 0 || 1
player = random.nextInt(2);
Scanner scan = new Scanner(System.in);
scan.useDelimiter(System.lineSeparator());
while(true) {
System.out.println();
printPlayer(player);
//Board output to console
printBord(bord);
//Draw when the board is full
if(turn >= 42) {
System.out.println("draw");
break;
}
//Input and validation from the console
System.out.println("number(0~6)Please enter.");
String line = scan.next();
if(line.trim().length() == 0 || line.trim().length() > 1) {
System.out.println("The input is invalid.");
continue;
}
if(!Character.isDigit(line.charAt(0))) {
System.out.println("The input is invalid.");
continue;
}
int num = Integer.parseInt(line);
if(num < 0 || num > 6) {
System.out.println("The input is invalid.");
continue;
}
for(i = 0; i < 6; i++) {
if(bord[i][num] == -1) {
bord[i][num] = player;
break;
}
}
if(i >= 6) {
System.out.println("[ " + num + "]Can't put any more in.");
continue;
}
if(isWin(bord, i , num, player)) {
System.out.println();
printBord(bord);
System.out.println((player == 0 ? "player1[○]":"player2[×]")+"Wins!!");
break;
}
player = (player * -1) + 1;
turn++;
}
scan.close();
System.out.println("End");
}
public static void printPlayer(int player) {
switch(player){
case 0:
System.out.println("player1[○]It's your turn.");
break;
case 1:
System.out.println("player2[×]It's your turn.");
break;
default:
System.out.println("error");
return;
}
}
public static void printBord(int[][] bord) {
for(int j = 0; j < 7; j++) {
if(j == 6) {
System.out.println("[ " + j + "]");
}else {
System.out.print("[ " + j + "]");
}
}
for(int i = 5; i >= 0; i--) {
for(int j = 0; j < 7; j++) {
String a = "";
a = (bord[i][j] == 0) ? "[○]" : (bord[i][j] == 1) ? "[×]" : "[ ]";
if(j == 6) {
System.out.println(a);
}else {
System.out.print(a);
}
}
}
}
public static boolean isWin(int[][] bord, int i, int j, int player) {
int yoko = 1;// -
int tate = 1;// |
int sura = 1;// /
int back = 1;// \
int y = i;
int x = j;
//Horizontal check(right)
while(x < 6) {
if(bord[i][x+1] == player) {
yoko++;
}else {
break;
}
x++;
}
x = j;
//Horizontal check(left)
while(x > 0) {
if(bord[i][x-1] == player) {
yoko++;
}else {
break;
}
x--;
}
if(yoko >= 4) {
return true;
}
//Vertical check(Up)
while(y < 5) {
if(bord[y+1][j] == player) {
tate++;
}else {
break;
}
y++;
}
y = i;
//Vertical check(under)
while(y > 0) {
if(bord[y-1][j] == player) {
tate++;
}else {
break;
}
y--;
}
if(tate >= 4) {
return true;
}
y = i;
x = j;
// /Direction check(Up)
while(y < 5 && x < 6) {
if(bord[y+1][x+1] == player) {
sura++;
}else {
break;
}
y++;
x++;
}
y = i;
x = j;
// /Direction check(under)
while(y > 0 && x > 0) {
if(bord[y-1][x-1] == player) {
sura++;
}else {
break;
}
y--;
x--;
}
if(sura >= 4) {
return true;
}
y = i;
x = j;
// \Direction check(Up)
while(y < 5 && x > 0) {
if(bord[y+1][x-1] == player) {
back++;
}else {
break;
}
y++;
x--;
}
y = i;
x = j;
// \Direction check(under)
while(y > 0 && x < 6) {
if(bord[y-1][x+1] == player) {
back++;
}else {
break;
}
y--;
x++;
}
if(back >= 4) {
return true;
}
return false;
}
}
That's all the sauce! To briefly explain the overall flow,
Judge in the up / down / left / right, slash, and backslash directions from the piece you placed.
tate = 1; yoko = 1; sura = 1; back = 1;
Loop for each of the following conditions
① If the direction is the player's piece when viewed from the current position tate++; Loop by shifting the current position in the ↑ direction If they are different, they will not be continuous, so the loop ends
② If the direction is the player's piece when viewed from the current position tate++; Loop by shifting the current position in the ↓ direction If they are different, they will not be continuous, so the loop ends
The results of ① and ② are tate = Number of continuous pieces in the ↑ direction + Number of continuous pieces in the ↓ direction + 1 (initial value); Will be the same as.
When applied in the ← → ↙↗↖↘ direction in the same way as ① and ② yoko = ← + → + 1; sura = ↙ + ↗ + 1; back = ↖ + ↘ + 1; Is obtained.
If any one of them meets the condition of> = 4, the current player wins.
The point to note is to set each variable to the initial value 1 and to see the next cell from the current position under the loop condition. This is because the reference points will overlap if the judgment is made based on the current position. (If the initial value is 0 and the final value is -1, there is no problem with the reference point.) Example: Let's look at [3] [3] as a reference point (Upward judgment) Increment because the current position [3] [3] is player Move upward and current position [4] [3] ... (Downward judgment) Increment because the current position [3] [3] is player Move downward and current position [2] [3] ...
You will be covered at your current position [3] [3].
You should be able to play immediately if you copy and paste! Four-in-a-row with gravity has a wave of petit boom since I played when I was a student (laugh) Please play while killing time!
Recommended Posts