(I know that the code is not the best;) ,but its my first real stuff so ill get better)
download- game&code
using System;
using System.Collections.Generic;
using System.Text;
namespace x_igol
{
class Program
{
public static char[,] board(char[,] a)
{
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
a[i, j] = ' ';
}
}
return a;
}
public static char[,] play(char[,] a,char b,string option,int level)
{
if (option == "vs player")
{
Console.WriteLine("\nchoose the place you want-1(up left),9(right bottom)\n");
int c = int.Parse(Console.ReadLine());
while (!iffree(a, c))
{
Console.WriteLine("\nyou choose place that allready taken place choose again:\n");
c = int.Parse(Console.ReadLine());
}
return put(a, c, b);
}
else
{
return think(a, level);
}
}
public static bool checknotfull(char[,] a)
{
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
if(a[i,j]==' ')
return true;
}
}
return false;
}
public static int torcount(char[,] a)
{
int c = 0;
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
if (a[i, j] != ' ')
c++;
}
}
return c;
}
public static char[,] think(char[,] a, int level)
{//computer sign-X
int p = 0;
Random r = new Random();
if (level == 1)
{
//check if the pc can win
p = checkchance(a, 'X');
if (p != 0)
{
return put(a, p, 'X');
}
p = checkchance(a, 'O');
// check if the player can win and if yes block him
if (p != 0)
{
return put(a, p, 'X');
}
//if not choose random number to put x in
p = r.Next(9) + 1;
while (!iffree(a, p))
{
p = r.Next(9) + 1;
}
return put(a, p, 'X');
}
else
{
if (level == 2)
{
if (torcount(a) == 0)
{
return put(a, 5, 'X');
}
else
{
//check if the pc can win
p = checkchance(a, 'X');
if (p != 0)
{
return put(a, p, 'X');
}
p = checkchance(a, 'O');
// check if the player can win and if yes block him
if (p != 0)
{
return put(a, p, 'X');
}
return put(a, (pcways(a, level)), 'X');
}
}
}
return a;
}
public static int pcways(char[,] a, int way)
{
if (way == 2)
{
return pcway2(a, torcount(a));
}
else
return 0;
}
public static int pcway2(char[,] a,int tor)
{
Random r=new Random();
int p = 0;
if (tor < 3)
{
if (a[0, 1].Equals('O') || a[2, 1].Equals('O') || a[1, 0].Equals('O') || a[2, 1].Equals('O'))
{
if (a[0, 1].Equals('O') || a[2, 1].Equals('O'))
{
return ((r.Next(2) + 2) * 2);
}
else
{
return (2 + (r.Next(2) * 6));
}
}
else
{
if (a[0, 0].Equals('O') || a[2, 2].Equals('O') || a[2, 0].Equals('O') || a[2, 0].Equals('O'))
{
if (a[0, 0] == 'O')
return 9;
else
{
if (a[2, 2] == 'O')
return 1;
else
{
if (a[2, 0] == 'O')
return 3;
else
return 7;
}
}
}
}
}
else
{
if (tor < 5)
{
if (a[0, 1].Equals('X') || a[2, 1].Equals('X') || a[1, 0].Equals('X') || a[1, 2].Equals('X'))
{
if (a[0, 1] == 'O')
return (1 + (r.Next(2) * 2));
else
{
if (a[2, 1] == 'O')
return (7 + (r.Next(2) * 2));
else
{
if (a[1, 0] == 'O')
return (1 + (r.Next(2) * 6));
else
return (3 + (r.Next(2) * 6));
}
}
}
else
{
//check if the pc can win
p = checkchance(a, 'X');
if (p != 0)
{
return p;
}
else
{
p = checkchance(a, 'O');
// check if the player can win and if yes block him
if (p != 0)
{
return p;
}
else
{
p = r.Next(9) + 1;
while (!iffree(a, p))
{
p = r.Next(9) + 1;
}
return p;
}
}
}
}
else
{
//check if the pc can win
p = checkchance(a, 'X');
if (p != 0)
{
return p;
}
else
{
p = checkchance(a, 'O');
// check if the player can win and if yes block him
if (p != 0)
{
return p;
}
else
{
p = r.Next(9) + 1;
while (!iffree(a, p))
{
p = r.Next(9) + 1;
}
return p;
}
}
}
}
return 0;
}
public static int checkchance(char[,] a,char c)
{
char empty=' ';
for (int i = 0; i < a.GetLength(0); i++)
{
if ((a[i, 0] == a[i, 1]) && ((a[i, 0] == c)&&(a[i,2]==empty)))
return ((i + 1) * 3);
if ((a[i, 1] == a[i, 2]) && ((a[i, 1] == c)&&(a[i,0]==empty)))
return (((i + 1) * 3) -2);
if ((a[i, 0] == a[i, 2]) && ((a[i, 0] == c) && (a[i, 1] == empty)))
return (((i + 1) * 3) - 1);
}
for (int i = 0; i < a.GetLength(1); i++)
{
if ((a[0, i] == a[1, i]) && (a[0, i] == c) && (a[2, i] == empty))
return (i + 7);
if ((a[1, i] == a[2, i]) && (a[1, i] == c)&&(a[0, i] == empty))
return (i + 1);
if ((a[0, i] == a[2, i]) && (a[0, i] == c)&&(a[1, i] == empty))
return (i + 4);
}
//אלכסונים-ראשי
if ((a[0, 0] == a[1, 1]) && (a[1, 1] == c) && (a[2, 2] == empty))
{
return 9;
}
if ((a[2, 2] == a[1, 1]) && (a[1, 1] == c) && (a[0, 0] == empty))
{
return 1;
}
if ((a[0, 0] == a[2, 2]) && (a[2, 2] == c) && (a[1, 1] == empty))
{
return 5;
}
//אלכסונים-משני
if ((a[2, 0] == a[1, 1]) && (a[1, 1] == c) && (a[0, 2] == empty))
{
return 3;
}
if ((a[0, 2] == a[1, 1]) && (a[1, 1] == c) && (a[2, 0] == empty))
{
return 7;
}
if ((a[2, 0] == a[0, 2]) && (a[0, 2] == c) && (a[1, 1] == empty))
{
return 5;
}
return 0;
}
public static void print(char[,] a)
{
Console.WriteLine("\n");
Console.WriteLine(a[0, 0] + "|" + a[0, 1] + "|" + a[0, 2]);
Console.WriteLine("------");
Console.WriteLine(a[1, 0] + "|" + a[1, 1] + "|" + a[1, 2]);
Console.WriteLine("------");
Console.WriteLine(a[2, 0] + "|" + a[2, 1] + "|" + a[2, 2]);
}
public static char[,] put(char[,] a, int c,char b)
{
int i = ((c-1) / 3);
int j = ((c - 1) % 3);
a[i, j] = b;
return a;
}
public static bool iffree(char[,] a, int c)
{
int i = ((c - 1) / 3);
int j = ((c-1) % 3);
if (a[i, j] == ' ')
return true;
else
return false;
}
public static void pvp()
{
char[,] game = new char[3, 3];
game = board(game);
bool win = false, win2 = false, tie = false;
print(game);
while(!win&&!win2&&!tie)
{
Console.WriteLine("\nplayer 1 turn:\n");
game = play(game, 'X', "vs player",0);
print(game);
tie = (!checknotfull(game));
if (checkwin(game))
{
win = true;
}
if (!win&&!tie)
{
Console.WriteLine("\nplayer 2 turn:\n");
game = play(game, 'O', "vs player",0);
print(game);
if (checkwin(game))
{
win2 = true;
}
}
}
if (win)
Console.WriteLine("\n\nPlayer 1 wins!;)");
else
{
if (win2)
{
Console.WriteLine("\n\nPlayer 2 wins!;)");
}
else
Console.WriteLine("Its a tie!;)");
}
Console.ReadLine();
}
public static void pvc(int level)
{
char[,] game = new char[3, 3];
game = board(game);
print(game);
bool win = false, win2 = false, tie = false;
while (!win && !win2 && !tie)
{
Console.WriteLine("\nComputer's turn:\n");
game = play(game, 'X', "vs computer",level);
print(game);
tie = (!checknotfull(game));
if (checkwin(game))
{
win = true;
}
if (!win&&!tie)
{
Console.WriteLine("\nYour's turn:\n");
game = play(game, 'O', "vs player",0);
print(game);
if (checkwin(game))
{
win2 = true;
}
}
}
if (win)
Console.WriteLine("\n\nComputer wins!;)");
else
{
if (win2)
{
Console.WriteLine("\n\nPlayer 2 wins!;)");
}
else
Console.WriteLine("Its a tie!;)");
}
Console.ReadLine();
}
public static bool checkwin(char[,] a)
{
for (int i = 0; i < a.GetLength(0); i++)
{
if (a[i, 0] != ' ')
{
if ((a[i, 0] == a[i, 1]) && (a[i, 1] == a[i, 2]))
return true;
}
}
for (int i = 0; i < a.GetLength(1); i++)
{
if (a[0, i] != ' ')
{
if ((a[0, i] == a[1, i]) && (a[1, i] == a[2, i]))
return true;
}
}
if (a[1, 1] != ' ')
{
if ((a[0, 0] == a[1, 1]) && (a[1, 1] == a[2, 2]))
return true;
if ((a[0, 2] == a[1, 1]) && (a[1, 1] == a[2, 0]))
return true;
}
return false;
}
static void Main(string[] args)
{
Console.WriteLine("Hi wellcome to x-igol\n");
Console.WriteLine("press:\n1-vs player\n2-vs the computer\n\n");
char c = char.Parse(Console.ReadLine());
while (c != '1' && c != '2')
{
Console.WriteLine("you press wrong number please press:\n1-vs player\n2-vs the computer\n\n");
c = char.Parse(Console.ReadLine());
}
if (c == '1')
{
pvp();
}
else
{
Console.WriteLine("\nchoose lv easy(press 1) or hard(press 2)");
char c2 = char.Parse(Console.ReadLine());
while (c2 != '1' && c2 != '2')
{
Console.WriteLine("\nyou press wrong number please press:\n1-easy\n2-hard\n\n");
c2 = char.Parse(Console.ReadLine());
}
if (c2 == '1')
pvc(1);
else
{
Random r = new Random();
pvc(2);
//pvc((r.Next(2)+2));
}//there is 2 ways to play hard level,r.next is choose 2 or 3
}
}
}
}

New Topic/Question
Reply




MultiQuote




|