You can set any size of the field, you can set how much you need to put in a row to win. I think I got a pretty flexible program. What do you think about the code? I would like to know if I made mistakes in program design.
skeleton:
public class Main {
private static String[] turn;
private static int hmInARowToWin;
private static Scanner sc = new Scanner(System.in);
private static pole[][] area;
public static void main(String[] args) {
System.out.println("Hi, lets play tik tak toe!");
while(true) {
System.out.println("Pick x.length and y.length of area(print"+'"'+"x y"+'"'+"): ");
turn = sc.nextLine().split(" ");
if(turn.length!=2) {
System.out.println("print: "+'"'+"x y"+'"'+"!");
}
else if(!isNumeric(turn[0])||!isNumeric(turn[1])) {
System.out.println("x and y should be numbers!");
}
else if(Integer.parseInt(turn[0])<=0||Integer.parseInt(turn[1])<=0) {
System.out.println("x and y should be >0!");
}
else {
area = new pole[Integer.parseInt(turn[0])][Integer.parseInt(turn[1])];
break;
}
}
fillAreaNOTHING();
String hmInARowToWinString;
while(true) {
System.out.println("hm in a row?");
hmInARowToWinString = sc.nextLine();
if(!isNumeric(hmInARowToWinString)) {
System.out.println("it should be a number!");
}
else if(Integer.parseInt(hmInARowToWinString)<3) {
System.out.println("it should be >2");
}
else if(Integer.parseInt(hmInARowToWinString)>Math.max(area.length,area[0].length)) {
System.out.println("u can not win");
}
else {
hmInARowToWin=Integer.parseInt(hmInARowToWinString);
break;
}
}
player winner;
while(true) {
printArea();
thisPlayerTurn(player.FIRST);
if(checkforWin(player.FIRST)) {
winner = player.FIRST;
break;
}
printArea();
thisPlayerTurn(player.SECOND);
if(checkforWin(player.SECOND)) {
winner = player.SECOND;
break;
}
}
printArea();
System.out.println("u won, "+winner.name);
}
private static void thisPlayerTurn(player p) {
System.out.println(p.name+" player, yours turn!(print: "+'"'+"x y"+'"'+")");
while(true) {
turn = sc.nextLine().split(" ");
if(turn.length!=2) {
System.out.println("print: "+'"'+"x y"+'"'+"!");
}
else if(!isNumeric(turn[0])||!isNumeric(turn[1])) {
System.out.println("x and y should be numbers!");
}
else if(!isXandYIn()) {
System.out.println("x and y should be in area! P.S.: area.lengthY="+area.length+", area.lengthX="+area[0].length);
}
else if(area[Integer.parseInt(turn[1])][Integer.parseInt(turn[0])]!=pole.NOTHING){
System.out.println("this place is already taken! Take another.");
}
else {
if(p==player.FIRST) {
area[Integer.parseInt(turn[1])][Integer.parseInt(turn[0])] = pole.X;
}
else { //second
area[Integer.parseInt(turn[1])][Integer.parseInt(turn[0])] = pole.O;
}
break;
}
}
}
private static boolean checkforWin(player p) {
pole toCheck;
if(p==player.FIRST) {
toCheck = pole.X;
}
else { //second
toCheck = pole.O;
}
for(int y=0;y<area.length;y++) {
for(int x=0;x<area[0].length;x++) {
if(area[y][x]!=toCheck) {continue;}
if(checkHorizontal(x,y,toCheck)||checkVertical(x,y,toCheck)||checkRightUpDiagonal(x,y,toCheck)||checkLeftUpDiagonal(x,y,toCheck)) {
return true;
}
}
}
return false;
}
private static boolean checkLeftUpDiagonal(int x, int y,pole toPick);
private static boolean checkRightUpDiagonal(int x, int y,pole toPick);
private static boolean checkVertical(int x, int y,pole toPick);
private static boolean checkHorizontal(int x, int y,pole toPick);
private static boolean isXandYIn() {}
public static boolean isNumeric(String strNum);
public static void printArea();
static void fillAreaNOTHING();
private static String repeat(int times,String string);
enum pole{
X,O,NOTHING
}
enum player{
FIRST("first"),SECOND("second");
String name;
player(String name){
this.name = name;
}
}
}
full code:
public class Main {
private static String[] turn;
private static int hmInARowToWin;
private static Scanner sc = new Scanner(System.in);
private static pole[][] area;
public static void main(String[] args) {
System.out.println("Hi, lets play tik tak toe!");
while(true) {
System.out.println("Pick x.length and y.length of area(print"+'"'+"x y"+'"'+"): ");
turn = sc.nextLine().split(" ");
if(turn.length!=2) {
System.out.println("print: "+'"'+"x y"+'"'+"!");
}
else if(!isNumeric(turn[0])||!isNumeric(turn[1])) {
System.out.println("x and y should be numbers!");
}
else if(Integer.parseInt(turn[0])<=0||Integer.parseInt(turn[1])<=0) {
System.out.println("x and y should be >0!");
}
else {
area = new pole[Integer.parseInt(turn[0])][Integer.parseInt(turn[1])];
break;
}
}
fillAreaNOTHING();
String hmInARowToWinString;
while(true) {
System.out.println("hm in a row?");
hmInARowToWinString = sc.nextLine();
if(!isNumeric(hmInARowToWinString)) {
System.out.println("it should be a number!");
}
else if(Integer.parseInt(hmInARowToWinString)<3) {
System.out.println("it should be >2");
}
else if(Integer.parseInt(hmInARowToWinString)>Math.max(area.length,area[0].length)) {
System.out.println("u can not win");
}
else {
hmInARowToWin=Integer.parseInt(hmInARowToWinString);
break;
}
}
player winner;
while(true) {
printArea();
thisPlayerTurn(player.FIRST);
if(checkforWin(player.FIRST)) {
winner = player.FIRST;
break;
}
printArea();
thisPlayerTurn(player.SECOND);
if(checkforWin(player.SECOND)) {
winner = player.SECOND;
break;
}
}
printArea();
System.out.println("u won, "+winner.name);
}
private static void thisPlayerTurn(player p) {
System.out.println(p.name+" player, yours turn!(print: "+'"'+"x y"+'"'+")");
while(true) {
turn = sc.nextLine().split(" ");
if(turn.length!=2) {
System.out.println("print: "+'"'+"x y"+'"'+"!");
}
else if(!isNumeric(turn[0])||!isNumeric(turn[1])) {
System.out.println("x and y should be numbers!");
}
else if(!isXandYIn()) {
System.out.println("x and y should be in area! P.S.: area.lengthY="+area.length+", area.lengthX="+area[0].length);
}
else if(area[Integer.parseInt(turn[1])][Integer.parseInt(turn[0])]!=pole.NOTHING){
System.out.println("this place is already taken! Take another.");
}
else {
if(p==player.FIRST) {
area[Integer.parseInt(turn[1])][Integer.parseInt(turn[0])] = pole.X;
}
else { //second
area[Integer.parseInt(turn[1])][Integer.parseInt(turn[0])] = pole.O;
}
break;
}
}
}
private static boolean checkforWin(player p) {
pole toCheck;
if(p==player.FIRST) {
toCheck = pole.X;
}
else { //second
toCheck = pole.O;
}
for(int y=0;y<area.length;y++) {
for(int x=0;x<area[0].length;x++) {
if(area[y][x]!=toCheck) {continue;}
if(checkHorizontal(x,y,toCheck)||checkVertical(x,y,toCheck)||checkRightUpDiagonal(x,y,toCheck)||checkLeftUpDiagonal(x,y,toCheck)) {
return true;
}
}
}
return false;
}
private static boolean checkLeftUpDiagonal(int x, int y,pole toPick) {
/*
i mean:
X
X
X
*/
;
int leftUpDirection=0,rightDownDirection=0;
boolean areWeLookingOnTempX;
if(x>y) {
areWeLookingOnTempX=false;
}
else {
areWeLookingOnTempX=true;
}
for(int tempX = x-1,tempY = y-1;areWeLookingOnTempX ? tempX>=0 : tempY>=0;tempX--,tempY--) {
if(area[tempY][tempX]==toPick) {
leftUpDirection++;
}
else {
break;
}
}
if(area[0].length-x>area.length-y) {
areWeLookingOnTempX=false;
}
else {
areWeLookingOnTempX=true;
}
for(int tempX = x+1,tempY = y+1;areWeLookingOnTempX ? tempX<area[0].length : tempY<area.length;tempX++,tempY++) {
if(area[tempY][tempX]==toPick) {
rightDownDirection++;
}
else {
break;
}
}
return 1+leftUpDirection+rightDownDirection==hmInARowToWin;
}
private static boolean checkRightUpDiagonal(int x, int y,pole toPick) {
/*
i mean:
X
X
X
*/
int rightUpDirection=0,leftDownDirection=0;
boolean areWeLookingOnTempX;
if(area[0].length-x>y+1) {
areWeLookingOnTempX=false;
}
else {
areWeLookingOnTempX=true;
}
for(int tempX = x+1,tempY = y-1;areWeLookingOnTempX ? tempX<area[0].length:tempY>=0;tempX++,tempY--) {
if(area[tempY][tempX]==toPick) {
rightUpDirection++;
}
else {
break;
}
}
if(x+1>area.length-y) {
areWeLookingOnTempX=false;
}
else {
areWeLookingOnTempX=true;
}
for(int tempX = x-1,tempY = y+1;areWeLookingOnTempX ? tempX>=0:tempY<area.length;tempX--,tempY++) {
if(area[tempY][tempX]==toPick) {
leftDownDirection++;
}
else {
break;
}
}
return 1+rightUpDirection+leftDownDirection==hmInARowToWin;
}
private static boolean checkVertical(int x, int y,pole toPick) {
/*
i mean:
X
X
X
*/
int upDirection=0,downDirection=0;
for(int i=y-1;i>=0;i--) {
if(area[i][x]==toPick) {
upDirection++;
}
else {
break;
}
}
for(int i=y+1;i<area.length;i++) {
if(area[i][x]==toPick) {
downDirection++;
}
else {
break;
}
}
return 1+upDirection+downDirection==hmInARowToWin;
}
private static boolean checkHorizontal(int x, int y,pole toPick) {
/*
i mean:
X X X
*/
int rightDirection=0,leftDirection=0;
for(int i=x-1;i>=0;i--) {
if(area[y][i]==toPick) {
leftDirection++;
}
else {
break;
}
}
for(int i=x+1;i<area[0].length;i++) {
if(area[y][i]==toPick) {
rightDirection++;
}
else {
break;
}
}
return 1+rightDirection+leftDirection==hmInARowToWin;
}
private static boolean isXandYIn() {
int turnX = Integer.parseInt(turn[0]);
int turnY = Integer.parseInt(turn[1]);
if(turnX<0||area[0].length<=turnX) {
return false;
}
if(turnY<0||area.length<=turnY) {
return false;
}
return true;
}
public static boolean isNumeric(String strNum) {
try {
Integer.parseInt(strNum);
} catch (NumberFormatException | NullPointerException nfe) {
return false;
}
return true;
}
public static void printArea() {
if(area != null) {
for(int i=0;i<area.length;i++) {
if(i!=0) {
System.out.println(repeat(area[0].length-1,"----")+"---");
}
for(int b=0;b<area[0].length;b++) {
if(b!=0) {
System.out.print("|");
}
System.out.print(" ");
if(area[i][b]==pole.X) {
System.out.print("X");
}
else if(area[i][b]==pole.O) {
System.out.print("0");
}
else {
System.out.print(" ");
}
System.out.print(" ");
}
System.out.println();
}
}
}
static void fillAreaNOTHING() {
for(int i=0;i<area.length;i++) {
for(int b=0;b<area[0].length;b++) {
area[i][b] = pole.NOTHING;
}
}
}
private static String repeat(int times,String string) {
String newStr="";
for(int i=0;i<times;i++) {
newStr+=string;
}
return newStr;
}
enum pole{
X,O,NOTHING
}
enum player{
FIRST("first"),SECOND("second");
String name;
player(String name){
this.name = name;
}
}
}