Commit 005baca5 authored by kiana's avatar kiana

othelloProject

parents
Pipeline #4337 failed with stages
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
OthelloMap ground = new OthelloMap();
Scanner scanner = new Scanner(System.in);
Move players = new Move();
while (players.rules.checkEnd(ground.getNuts())) {
System.out.println("It's player1's turn, choose a place to put your nut");
if(!players.rules.pass( 2,ground.getNuts())){
int length=scanner.nextInt();
int width=(int)scanner.next().charAt(0)-'A'+1;
;
while(!players.rules.input(length,width, 2,ground.getNuts())){
System.out.println("wrong input");
length=scanner.nextInt();
width=(int)scanner.next().charAt(0)-'A'+1;
}
players.rules.setYes(0);
players.rules.setMojaz(0);
players.move( 2, length,width, ground.getNuts());
players.rules.setYes(0);
players.rules.setMojaz(0);
}
ground.map();
players.rules.score(ground.getNuts());
System.out.println("It's player2's turn, choose a place to put your nut");
if(!players.rules.pass( 1,ground.getNuts())) {
int length = scanner.nextInt();
int width = (int) scanner.next().charAt(0) - 'A' + 1;
while (!players.rules.input(length, width, 1, ground.getNuts())) {
System.out.println("wrong input");
length = scanner.nextInt();
width = (int) scanner.next().charAt(0) - 'A' + 1;
}
players.rules.setMojaz(0);
players.rules.setYes(0);
;
players.move(1, length, width, ground.getNuts());
}
players.rules.setMojaz(0);
players.rules.setYes(0);
ground.map();
players.rules.score(ground.getNuts());
players.rules.winner(ground.getNuts());
}
}
}
public class Move {
Rules rules = new Rules();
/**
* checks each direction to see if it can be placed there
* if yes then put the nut in the place
* and changes the nuts between to the right color
* @param turn
* @param x
* @param y
* @param map
*/
public void move(int turn, int x, int y, int[][] map) {
x--;
y--;
int i = 1;
//int[][] arr = map;
if (rules.ruleCheckRight(x, y,turn,map)) {
map[x][y]=turn;
while (map[x][y] != map[x + i][y]) {
if (turn == 1)
map[x + i][y] = 1;
else if (turn == 2)
map[x + i][y] = 2;
i++;
}
}
if (rules.ruleCheckLeft(x, y,turn,map)) {
map[x][y]=turn;
int i1 = 1;
while (map[x][y] != map[x - i1][y]) {
if (turn == 1)
map[x - i1][y] = 1;
else if (turn == 2)
map[x - i1][y] = 2;
i1++;
}
}
if (rules.ruleCheckUp(x, y,turn, map)) {
map[x][y]=turn;
int j = 1;
while (map[x][y] != map[x][y + j]) {
if (turn == 1)
map[x][y + j] = 1;
else if (turn == 2)
map[x][y + j] = 2;
j++;
}
}
if (rules.ruleCheckDown(x, y,turn, map)) {
map[x][y]=turn;
int j1 = 1;
while (map[x][y] != map[x][y - j1]) {
if (turn == 1)
map[x][y - j1] = 1;
else if (turn == 2)
map[x][y - j1] = 2;
j1++;
}
}
if (rules.ruleCheckUpRight(x, y,turn, map)) {
map[x][y]=turn;
int k = 1, z = 1;
while (map[x][y] != map[x + k][y + z]) {
if (turn == 1)
map[x + k][y + z] = 1;
else if (turn == 2)
map[x + k][y + z] = 2;
k++;
z++;
}
}
if (rules.ruleCheckUpLeft(x, y,turn, map)) {
map[x][y]=turn;
int k = 1, z = 1;
while (map[x][y] != map[x - k][y + z]) {
if (turn == 1)
map[x - k][y + z] = 1;
else if (turn == 2)
map[x - k][y + z] = 2;
k++;
z++;
}
}
if (rules.ruleCheckDownRight(x, y,turn, map)) {
map[x][y]=turn;
int k = 1, z = 1;
while (map[x][y] != map[x + k][y - z]) {
if (turn == 1)
map[x + k][y - z] = 1;
else if (turn == 2)
map[x + k][y - z] = 2;
k++;
z++;
}
}
if (rules.ruleCheckDownLeft(x, y,turn, map)) {
map[x][y]=turn;
int k = 1, z = 1;
while (map[x][y] != map[x - k][y - z]) {
if (turn == 1)
map[x - k][y - z] = 1;
else if (turn == 2)
map[x - k][y - z] = 2;
k++;
z++;
}
}
if(rules.getYes() == 1)
map[x][y] = turn;
//map(arr);
rules.setMojaz(0);
rules.setYes(0);
}
}
import java.io.*;
public class OthelloMap {
private int[][] nuts = new int[8][8];
public OthelloMap(){
meghdardehi();
map1();
}
private final String resetcolor = "\u001B[0m";
private final String color2 = "\u001B[38;5;27m";
private final String color3 = "\u001B[48;5;197m";
private final String color1 = "\u001B[48;5;243m";
private final String resetbg = "\u001B[49m";
public int[][] getNuts() {
return nuts;
}
public void setNuts(int[][] nuts) {
this.nuts = nuts;
}
public void meghdardehi(){
for(int i=0; i<8; i++) {
for (int j = 0; j < 8; j++) {
nuts[i][j] = 0;
}
}
nuts[3][3] = 1;
nuts[4][4] = 1;
nuts[3][4] = 2;
nuts[4][3] = 2;
}
/**
* prints the table alphabets
* prints the map
* prints the table numbers
*/
public void map(){
for(char alphabet= 'A'; alphabet<= 'H'; alphabet++){
System.out.print(" "+ alphabet +" ");
}
System.out.println(" ");
int number = 1;
System.out.print(" ");
for(int i=0; i<8; i++){
for(int j=0; j<8; j++){
// System.out.print(color1+color2+"----------"+resetcolor+resetbg);
System.out.print(color3+color2+" "+resetcolor+resetbg);
}
System.out.println(color3+" "+resetcolor);
System.out.print(" ");
for(int j=0; j<8; j++){
// System.out.print(color1+color2+"|"+resetcolor+resetbg);
System.out.print(color3+" "+resetbg);
if(nuts[i][j] == 0)
System.out.print(color1+" "+resetbg);
else if(nuts[i][j] == 1)
System.out.print(color1+" "+ "\u26AB " + " "+resetbg);
else if(nuts[i][j] == 2)
System.out.print( color1+" "+ "\u26AA " + " "+resetbg);
}
System.out.println(color3+" "+resetbg);
System.out.print(number++ + " ");
for(int j=0; j<9; j++){
// System.out.print(color1+color2+"|"+resetcolor+resetbg);
System.out.print(color3+" "+resetcolor);
if(j==8){
System.out.print(" ");
}
else{
System.out.print(color1+" "+resetbg);
}}
System.out.println(" ");
System.out.print(" ");
}
/**
* last line
*/
for(int j=0; j<8; j++){
// System.out.print(color1+color2 +"----------"+resetcolor+resetbg);
System.out.print(color3+color2 +" "+resetcolor+resetbg);
}
System.out.println(color3+" "+resetbg);
}
/**
* first map of the game
*/
public void map1(){
nuts[3][3] = 1;
nuts[4][4] = 1;
nuts[3][4] = 2;
nuts[4][3] = 2;
map();
}
}
public class Rules {
private int mojaz = 0, yes = 0;
/**
* gets the confirmation
* @return
*/
public int getMojaz() {
return mojaz;
}
/**
* sets the confirmation
* @param mojaz
*/
public void setMojaz(int mojaz) {
this.mojaz = mojaz;
}
/**
* gets approved state
* @return
*/
public int getYes() {
return yes;
}
/**
* sets approved state
* @param yes
*/
public void setYes(int yes) {
this.yes = yes;
}
/**
* puts the nut in the given place
* move to the right to see if there is a same color nut in this row
* checks if every place in its way are full
* returns if it is allowed to put the nut in that place
* removes the nut from that place
* @param x
* @param y
* @param type
* @param map
* @return
*/
public boolean ruleCheckRight(int x, int y, int type, int[][] map) {
// System.out.println("1");
int count = 1;
//int[][] map = ground.getNuts();
if (map[x][y] == 1 || map[x][y] == 2)
return false;
map[x][y] = type;
for (int i = x + 1; i < 8; i++) {
if (map[i][y] == 0)
break;
else {
if (map[x][y] != map[i][y]) {
count++;
}
}
if (map[x][y] == map[i][y] && count == i - x && count != 1) {
map[x][y] = 0;
mojaz = 1;
yes = 1;
return true;
}
}
map[x][y] = 0;
return false;
}
/**
* puts the nut in the given place
* move to the left to see if there is a same color nut in this row
* checks if every place in its way are full
* returns if it is allowed to put the nut in that place
* removes the nut from that place
* @param x
* @param y
* @param type
* @param map
* @return
*/
public boolean ruleCheckLeft(int x, int y, int type, int[][] map) {
int count = 1;
// System.out.println("2");
//int[][] map = ground.getNuts();
if (mojaz == 1)
map[x][y] = 0;
if (map[x][y] == 1 || map[x][y] == 2)
return false;
map[x][y] = type;
for (int i = x - 1; i >= 0; i--) {
if (map[i][y] == 0)
break;
else {
if (map[x][y] != map[i][y]) {
count++;
}
}
if (map[x][y] == map[i][y] && count == x - i && count != 1) {
map[x][y] = 0;
mojaz = 1;
yes = 1;
return true;
}
}
map[x][y] = 0;
return false;
}
/**
* puts the nut in the given place
* move to the up to see if there is a same color nut in this row
* checks if every place in its way are full
* returns if it is allowed to put the nut in that place
* removes the nut from that place
* @param x
* @param y
* @param type
* @param map
* @return
*/
public boolean ruleCheckUp(int x, int y, int type, int[][] map) {
int count = 1;
// System.out.println("3");
//int[][] map = ground.getNuts();
if (mojaz == 1)
map[x][y] = 0;
if (map[x][y] == 1 || map[x][y] == 2)
return false;
map[x][y] = type;
for (int j = y + 1; j < 8; j++) {
if (map[x][j] == 0)
break;
else {
if (map[x][y] != map[x][j]) {
count++;
}
}
if (map[x][y] == map[x][j] && count == j - y && count != 1) {
map[x][y] = 0;
mojaz = 1;
yes = 1;
return true;
}
}
map[x][y] = 0;
return false;
}
/**
* puts the nut in the given place
* move to the down to see if there is a same color nut in this row
* checks if every place in its way are full
* returns if it is allowed to put the nut in that place
* removes the nut from that place
* @param x
* @param y
* @param type
* @param map
* @return
*/
public boolean ruleCheckDown(int x, int y, int type, int[][] map) {
int count = 1;
// System.out.println("4");
//int[][] map = ground.getNuts();
if (mojaz == 1)
map[x][y] = 0;
if (map[x][y] == 1 || map[x][y] == 2)
return false;
map[x][y] = type;
for (int j = y - 1; j >= 0; j--) {
if (map[x][j] == 0)
break;
else {
if (map[x][y] != map[x][j]) {
count++;
}
}
if (map[x][y] == map[x][j] && count == y - j && count != 1) {
map[x][y] = 0;
mojaz = 1;
yes = 1;
return true;
}
}
map[x][y] = 0;
return false;
}
/**
* puts the nut in the given place
* move to the up right to see if there is a same color nut in this row
* checks if every place in its way are full
* returns if it is allowed to put the nut in that place
* removes the nut from that place
* @param x
* @param y
* @param type
* @param map
* @return
*/
public boolean ruleCheckUpRight(int x, int y, int type, int[][] map) {
int count = 1;
// System.out.println("8");
//int[][] map = ground.getNuts();
int i = x + 1, j = y + 1;
if (mojaz == 1)
map[x][y] = 0;
if (map[x][y] == 1 || map[x][y] == 2)
return false;
map[x][y] = type;
while (i < 8 && j < 8) {
if (map[i][j] == 0)
break;
else {
if (map[x][y] != map[i][j]) {
count++;
}
}
if ((map[x][y] == map[i][j] && count == j - y) && (map[x][y] == map[i][j] && count == x - i) && count != 1) {
map[x][y] = 0;
mojaz = 1;
yes = 1;
return true;
}
i++;
j++;
}
map[x][y] = 0;
return false;
}
/**
* puts the nut in the given place
* move to the up left to see if there is a same color nut in this row
* checks if every place in its way are full
* returns if it is allowed to put the nut in that place
* removes the nut from that place
* @param x
* @param y
* @param type
* @param map
* @return
*/
public boolean ruleCheckUpLeft(int x, int y, int type, int[][] map) {
int count = 1;
// System.out.println("5");
//int[][] map = ground.getNuts();
int i = x - 1, j = y + 1;
if (mojaz == 1)
map[x][y] = 0;
if (map[x][y] == 1 || map[x][y] == 2)
return false;
map[x][y] = type;
while (i >= 0 && j < 8) {
if (map[i][j] == 0)
break;
else {
if (map[x][y] != map[i][j]) {
count++;
}
}
if ((map[x][y] == map[i][j] && count == j - y) &&
(map[x][y] == map[i][j] && count == x - i) && count != 1) {
map[x][y] = 0;
mojaz = 1;
yes = 1;
return true;
}
i--;
j++;
}
map[x][y] = 0;
return false;
}
/**
* puts the nut in the given place
* move to the down right to see if there is a same color nut in this row
* checks if every place in its way are full
* returns if it is allowed to put the nut in that place
* removes the nut from that place
* @param x
* @param y
* @param type
* @param map
* @return
*/
public boolean ruleCheckDownRight(int x, int y, int type, int[][] map) {
int count = 1;
// System.out.println("6");
//int[][] map = ground.getNuts();
int i = x + 1, j = y - 1;
if (mojaz == 1)
map[x][y] = 0;
if (map[x][y] == 1 || map[x][y] == 2)
return false;
map[x][y] = type;
while (i < 8 && j >= 0) {
if (map[i][j] == 0)
break;
else {
if (map[x][y] != map[i][j]) {
count++;
}
}
if ((map[x][y] == map[i][j] && count == y - j) &&
(map[x][y] == map[i][j] && count == i - x) && count != 1) {
map[x][y] = 0;
mojaz = 1;
yes = 1;
return true;
}
i++;
j--;
}
map[x][y] = 0;
return false;
}
/**
* puts the nut in the given place
* move to the down left to see if there is a same color nut in this row
* checks if every place in its way are full
* returns if it is allowed to put the nut in that place
* removes the nut from that place
* @param x
* @param y
* @param type
* @param map
* @return
*/
public boolean ruleCheckDownLeft(int x, int y, int type, int[][] map) {
int count = 1;
// System.out.println("7");
//int[][] map = ground.getNuts();
int i = x - 1, j = y - 1;
if (mojaz == 1)
map[x][y] = 0;
if (map[x][y] == 1 || map[x][y] == 2)
return false;
map[x][y] = type;
while (i >= 0 && j >= 0) {
if (map[i][j] == 0)
break;
else {
if (map[x][y] != map[i][j]) {
count++;
}
}
if ((map[x][y] == map[i][j] && count == y - j) &&
(map[x][y] == map[i][j] && count == x - i) && count != 1) {
map[x][y] = 0;
mojaz = 1;
yes = 1;
return true;
}
i--;
j--;
}
map[x][y] = 0;
return false;
}
/**
* checks if the game has ended
* whether there is no empty place or one of the nuts has finished
* @param map
* @return
*/
public boolean checkEnd(int[][] map) {
int counter = 0, counter1 = 0, counter2 = 0;
//int[][] map = ground.getNuts();
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (map[i][j] == 0)
counter++;
else if(map[i][j] == 1)
counter1++;
else if(map[i][j] == 2)
counter2++;
}
}
return counter > 0 && counter1 != 0 && counter2 != 0;
}
/**
* prints scores of each player
* @param map
*/
public void score(int[][] map) {
int count1 = 0, count2 = 0;
//int[][] map = ground.getNuts();
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (map[i][j] == 1)
count1++;
else if (map[i][j] == 2)
count2++;
}
}
System.out.println("player 1: " + count1);
System.out.println("player 2: " + count2);
}
/**
* prints the winner
* @param map
*/
public void winner(int[][] map) {
int counter1 = 0, counter2 = 0;
//int[][] map = ground.getNuts();
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (map[i][j] == 1)
counter1++;
else if (map[i][j] == 2)
counter2++;
}
}
if (counter1 + counter2 == 64 || counter1 == 0 || counter2 == 0) {
if (counter1 > counter2)
System.out.println("player 1 wins");
else if (counter1 < counter2)
System.out.println("player 2 wins");
else
System.out.println("we have a tie");
}
}
public boolean all(int m, int n, int type, int[][] map){
setMojaz(0);
setMojaz(0);
if (ruleCheckDown(m, n, type, map) || ruleCheckUp(m, n, type, map) || ruleCheckRight(m, n, type, map) || ruleCheckLeft(m, n, type, map) ||
ruleCheckUpRight(m, n, type, map) || ruleCheckUpLeft(m, n, type, map) || ruleCheckDownRight(m, n, type, map) || ruleCheckDownLeft(m, n, type, map)) {
setYes(0);
setMojaz(0);
return true;
}
setYes(0);
setMojaz(0);
return false;
}
public boolean input(int x,int y,int type,int[][] map){
int check=0;
if(x>0&&x<=8)
check++;
if(y>0&&y<=8)
check++;
if(check==2&&all(x-1,y-1,type,map))
return true;
return false;
}
/**
* checks if there is no place for the player to put its nut then it prints pass and return true
* @param type
* @param map
* @return
*/
public boolean pass(int type, int[][] map) {
int counter = 0;
setYes(0);
setMojaz(0);
for (int n = 0; n < 8; n++) {
for (int m = 0; m < 8; m++) {
if(map[n][m] == 0) {
if (all(m, n, type, map))
counter++;
setYes(0);
setMojaz(0);
}
}
}
if (counter == 0) {
System.out.println("PASS");
return true;
}
return false;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment