Skip to content

Instantly share code, notes, and snippets.

@Viacheslav77
Last active October 12, 2015 14:53
Show Gist options
  • Save Viacheslav77/c15b758efc403de1e24c to your computer and use it in GitHub Desktop.
Save Viacheslav77/c15b758efc403de1e24c to your computer and use it in GitHub Desktop.
Крестики - нолики
package Krestiki;
import java.util.Scanner;
import java.util.Arrays;
import java.util.Random;
public class MyKtestic {
public static String [][] PlayingField =new String[3][3];
public static String gamer;
public static String xo;
public static String winner;
public static void main(String[] args) {
System.out.println("Игра крестикики - нолики.");
while (true) {
Begin ();
PrintField();
while (winner == null) {
Running ();
PrintField();
CheckWinner();
}
System.out.println(winner);
winner = null;
}
}
public static void Begin () {
Scanner xo2 = new Scanner (System.in);
for(int i=0;i<PlayingField.length;i++){
for(int j=0;j<PlayingField[0].length;j++)
{PlayingField[i][j]= "_";
}
}
System.out.println();
do {
System.out.println("Чем будете играть (o/x)? ");
gamer = xo2.nextLine();
} while(!(gamer.equals("o")||gamer.equals("x")));
if (gamer.equals("o")){
System.out.println("Вы выбрали о. Первый ход делает машина. ");
xo = "x";
gamer = "RunningMachine";
}
else {
System.out.println("Вы выбрали x. ходите первым. ");
xo= "x";
gamer = "RunningMan";
}
}
public static void Running () {
// Проверка возможности хода
winner = "Ничья :(";
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if(PlayingField[i][j].equals("_"))
winner = null;
break;
}
}
if ("RunningMan".equals(gamer)){
RunningMan(xo);
if (xo.equals("x")){
xo = "o";
}
else {
xo = "x";
}
gamer = "RunningMachine";
}
else {
RunningMachine(xo);
if (xo.equals("x")){
xo = "o";
}
else {
xo = "x";
}
gamer = "RunningMan";
}
}
public static void RunningMachine(String xo) {
int si;
int sj;
Random r = new Random ();
System.out.println("ход делает машина. ");
do {si=r.nextInt(3);
sj=r.nextInt(3);
} while(!(PlayingField[si][sj].equals("_")));
PlayingField[si][sj]= xo;
}
public static void RunningMan(String xo) {
int si;
int sj;
Scanner xo2 = new Scanner (System.in);
do {
System.out.println();
System.out.println("Ваш ход. ");
System.out.println("Введите номер ячейки по вертикали: ");
si = xo2.nextInt();
System.out.println("Введите номер ячейки по горизонтали: ");
sj = xo2.nextInt();
} while(!(si<=3 & sj<=3 && PlayingField[si-1][sj-1].equals("_")));
PlayingField[si-1][sj-1]= xo;
}
public static void CheckWinner() {
// Проверка
// по горизонтали
for (int i = 0; i < 3; i++) {
if (PlayingField[i][0] == PlayingField[i][1]
&& PlayingField[i][1] == PlayingField[i][2]
&& PlayingField[i][0] != "_") {
winner = gamer;
break;
}
}
// по вертикали
if (winner == null) {
for (int i = 0; i < 3; i++) {
if (PlayingField[0][i] == PlayingField[1][i]
&& PlayingField[1][i] == PlayingField[2][i]
&& PlayingField[0][i] != "_") {
winner = gamer;
break;
}
}
}
// по диагонали
if (winner == null) {
if (PlayingField[0][0] == PlayingField[1][1]
&& PlayingField[1][1] == PlayingField[2][2]
&& PlayingField[0][0] != "_") {
winner = gamer;
}
}
// по второй диагонали
if (winner == null) {
if (PlayingField[0][2] == PlayingField[1][1]
&& PlayingField[1][1] == PlayingField[2][0]
&& PlayingField[0][2] != "_") {
winner = gamer;
}
}
// проверка на победителя
if (winner != null){
if ("RunningMachine".equals(winner)){
winner = "Вы выграли ;)";
}
else if ("RunningMan".equals(winner)){
winner = "Вы проиграли :(";
}
else {
winner = "Ничья :(";
}
}
}
//блок печати
public static void PrintField() {
for( int i = 0;i<PlayingField.length;i++){
for(int j=0;j<PlayingField[0].length;j++)
{System.out.print(PlayingField[i][j]+" ");
}
System.out.println();
}
System.out.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment