Files

308 lines
12 KiB
C#
Raw Permalink Normal View History

2022-09-21 11:37:13 +02:00
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MonoGame.Extended;
2022-09-26 16:18:34 +02:00
using System;
2022-09-26 15:42:35 +02:00
using System.Collections.Generic;
2022-09-21 11:37:13 +02:00
namespace Tic_Tac_Toe {
public class Game1 : Game {
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
2022-09-23 11:27:45 +02:00
private Texture2D Circle, X;
2022-09-26 15:42:35 +02:00
private Texture2D CircleWonTexture, XWonTexture;
2022-09-21 11:37:13 +02:00
private RectangleF[,] rectArray;
2022-09-21 16:04:22 +02:00
private int[,] CircleXPostion;
2022-09-26 15:42:35 +02:00
private List<string> Circle4PosList; //for storing the 4 latest positions
private List<string> X4PosList;
2022-09-23 11:27:45 +02:00
private int lineThickness = 5;
private int playerWon = 0; //1 is circle 2 is x
2022-09-27 11:21:57 +02:00
private bool isCircleNext = false; //basically start player here
2022-09-21 11:37:13 +02:00
public Game1() {
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize() {
//Setting Resolution
2022-09-21 16:04:22 +02:00
_graphics.PreferredBackBufferWidth = 800;
_graphics.PreferredBackBufferHeight = 600;
_graphics.ApplyChanges();
2022-09-21 11:37:13 +02:00
//For field
rectArray = new RectangleF[3,3];
2022-09-21 16:04:22 +02:00
CircleXPostion = new int[3, 3]; //0 is free space, 1 is circle, 2 is X
2022-09-21 11:37:13 +02:00
2022-09-26 15:42:35 +02:00
Circle4PosList = new List<string>();
X4PosList = new List<string>();
2022-09-21 11:37:13 +02:00
base.Initialize();
}
protected override void LoadContent() {
_spriteBatch = new SpriteBatch(GraphicsDevice);
2022-09-21 16:04:22 +02:00
Circle = Content.Load<Texture2D>("Textures/circle");
X = Content.Load<Texture2D>("Textures/X");
2022-09-26 15:42:35 +02:00
CircleWonTexture = Content.Load<Texture2D>("Textures/Circle win text");
XWonTexture = Content.Load<Texture2D>("Textures/Cross win text");
2022-09-21 16:04:22 +02:00
2022-09-21 11:37:13 +02:00
}
protected override void Update(GameTime gameTime) {
2022-09-23 11:27:45 +02:00
if (playerWon == 0) {
var mouse = Mouse.GetState();
for (int i = 0; i < rectArray.GetLength(0); i++) {
for (int j = 0; j < rectArray.GetLength(1); j++) {
mouse = Mouse.GetState();
if (isCircleNext) {
2022-09-26 14:24:24 +02:00
if (rectArray[j, i].Contains(new Point(mouse.X, mouse.Y)) && mouse.LeftButton == ButtonState.Pressed && CircleXPostion[j, i] == 0) {
CircleXPostion[j, i] = 1; //eltároljuk a kör pozicióját ||| CHANGED FROM [i,j]
2022-09-23 11:27:45 +02:00
isCircleNext = false;
2022-09-26 16:18:34 +02:00
2022-09-23 11:39:57 +02:00
printGameStateArray();
2022-09-26 16:18:34 +02:00
2022-09-26 15:42:35 +02:00
//Put list stuff here
2022-09-26 16:18:34 +02:00
Circle4PosList.Add(j+";"+i);
if(Circle4PosList.Count == 4) {
string[] index = Circle4PosList[0].Split(';');
CircleXPostion[Convert.ToInt32(index[0]), Convert.ToInt32(index[1])] = 0; //set indexed field to empty
Circle4PosList.RemoveAt(0);
}
//printing list
printList(Circle4PosList);
2022-09-23 11:27:45 +02:00
//System.Threading.Thread.Sleep(250);
}
} else {
2022-09-26 14:24:24 +02:00
if (rectArray[j, i].Contains(new Point(mouse.X, mouse.Y)) && mouse.LeftButton == ButtonState.Pressed && CircleXPostion[j, i] == 0) {
CircleXPostion[j, i] = 2; //eltaroljuk az x poziciojat
2022-09-23 11:27:45 +02:00
isCircleNext = true;
2022-09-23 11:39:57 +02:00
printGameStateArray();
2022-09-26 16:18:34 +02:00
2022-09-26 15:42:35 +02:00
//Put list stuff here
2022-09-26 16:18:34 +02:00
X4PosList.Add(j+";"+i);
if (X4PosList.Count == 4) {
string[] index = X4PosList[0].Split(';');//(j;i)
CircleXPostion[Convert.ToInt32(index[0]), Convert.ToInt32(index[1])] = 0; //set indexed field to empty (j;i)
X4PosList.RemoveAt(0);
}
//printing list
printList(X4PosList);
2022-09-23 11:27:45 +02:00
//System.Threading.Thread.Sleep(250);
}
}
}
}
}
CheckGameCondition();
2022-09-21 11:37:13 +02:00
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime) {
2022-09-21 16:04:22 +02:00
GraphicsDevice.Clear(Color.CadetBlue);
2022-09-21 11:37:13 +02:00
2022-09-23 11:27:45 +02:00
var mouse = Mouse.GetState();
2022-09-21 11:37:13 +02:00
_spriteBatch.Begin();
2022-09-26 15:42:35 +02:00
if(playerWon == 1) {
_spriteBatch.Draw(CircleWonTexture, new Rectangle(10, 0, 50, 600), Color.White);
_spriteBatch.Draw(CircleWonTexture, new Rectangle(_graphics.PreferredBackBufferWidth - 75, 0, 50, 600), Color.White);
}else if(playerWon == 2) {
_spriteBatch.Draw(XWonTexture, new Rectangle(10, 0, 50, 600), Color.White);
_spriteBatch.Draw(XWonTexture, new Rectangle(_graphics.PreferredBackBufferWidth - 75, 0, 50, 600), Color.White);
}
2022-09-21 11:37:13 +02:00
2022-09-21 16:04:22 +02:00
DrawTicTacToeFieldRect(rectArray, Color.LightGray);
//checking if mouse is in a rect
2022-09-21 11:37:13 +02:00
for (int i = 0; i < rectArray.GetLength(0); i++) {
for (int j = 0; j < rectArray.GetLength(1); j++) {
2022-09-23 11:27:45 +02:00
mouse = Mouse.GetState();
2022-09-21 16:04:22 +02:00
if (isCircleNext) { //Circle player highlight
if (rectArray[i, j].Contains(new Point(mouse.X, mouse.Y))) {
DrawSingleRect(rectArray, Color.Red, i, j);
}
} else { //X player highlight
if (rectArray[i, j].Contains(new Point(mouse.X, mouse.Y))) {
DrawSingleRect(rectArray, Color.Green, i, j);
}
2022-09-21 11:37:13 +02:00
}
}
}
2022-09-21 16:04:22 +02:00
2022-09-23 11:27:45 +02:00
for (int i = 0; i < CircleXPostion.GetLength(0); i++) {
for (int j = 0; j < CircleXPostion.GetLength(1); j++) {
mouse = Mouse.GetState();
if (CircleXPostion[i,j] == 1) { //Circle player highlight
_spriteBatch.Draw(Circle, new Rectangle(200 * i + 100, 200*j, 200, 200), Color.White);
} else if (CircleXPostion[i, j] == 2) { //X player highlight
_spriteBatch.Draw(X, new Rectangle(200 * i + 100, 200*j, 200, 200), Color.White);
}
}
}
2022-09-26 15:42:35 +02:00
//Testing one texture draw
2022-09-23 11:27:45 +02:00
//loop through circle pos arr to draw the texture
/*_spriteBatch.Draw(Circle, new Rectangle(100, 0, 200, 200), Color.White);
_spriteBatch.Draw(X, new Rectangle(300, 0, 200, 200), Color.White);*/
2022-09-21 11:37:13 +02:00
_spriteBatch.End();
base.Draw(gameTime);
}
public void DrawTicTacToeField() {
//drawing the field
//Vertical Lines
2022-09-21 16:04:22 +02:00
_spriteBatch.DrawLine(250, 0, 250, _graphics.PreferredBackBufferHeight, Color.White, lineThickness, 0);
_spriteBatch.DrawLine(550, 0, 550, _graphics.PreferredBackBufferHeight, Color.White, lineThickness, 0);
2022-09-21 11:37:13 +02:00
//Horizontal Lines
2022-09-21 16:04:22 +02:00
_spriteBatch.DrawLine(0, 200, _graphics.PreferredBackBufferWidth, 200, Color.White, lineThickness, 0);
_spriteBatch.DrawLine(0, 400, _graphics.PreferredBackBufferWidth, 400, Color.White, lineThickness, 0);
2022-09-21 11:37:13 +02:00
}
private void DrawTicTacToeFieldRect(RectangleF[,] rectArray, Color color) {
//do a base rectangle, pass in a 2d rect array to get rect.contains
//loop through the array to draw each rect
const int size = 200;
for (int i = 0; i < rectArray.GetLength(0); i++) {
for (int j = 0; j < rectArray.GetLength(1); j++) {
rectArray[i, j] = new RectangleF(size * i + 100, size*j, size, size); //+100 is offset
2022-09-23 11:27:45 +02:00
_spriteBatch.DrawRectangle(rectArray[i, j], color, lineThickness, 0);
2022-09-21 11:37:13 +02:00
}
}
}
2022-09-21 16:04:22 +02:00
private void DrawSingleRect(RectangleF[,] rectArray, Color color, int indexX, int indexY) {
2022-09-23 11:27:45 +02:00
_spriteBatch.DrawRectangle(rectArray[indexX, indexY], color, lineThickness, 0);
}
2022-09-23 11:39:57 +02:00
private void printGameStateArray() {
for (int i = 0; i < CircleXPostion.GetLength(0); i++) {
for (int j = 0; j < CircleXPostion.GetLength(1); j++) {
2022-09-26 14:24:24 +02:00
System.Console.Write(CircleXPostion[j,i] +"(j:"+j+", i:"+i + ") | ");
2022-09-23 11:39:57 +02:00
}
System.Console.WriteLine();
}
2022-09-26 14:24:24 +02:00
System.Console.WriteLine("\n");
2022-09-23 11:39:57 +02:00
}
2022-09-26 16:18:34 +02:00
private void printList(List<string> list) {
foreach(var elem in list) {
System.Console.WriteLine(elem);
}
}
2022-09-23 11:27:45 +02:00
private void CheckGameCondition() {
//vertical
//circle
for (int i = 0; i < CircleXPostion.GetLength(0); i++) {
int count = 0;
for (int j = 0; j < CircleXPostion.GetLength(1); j++) {
if(CircleXPostion[i, j] == 1) {
count++;
}
}
if(count == 3) {
playerWon = 1;
return;
}
}
//x
for (int i = 0; i < CircleXPostion.GetLength(0); i++) {
int count = 0;
for (int j = 0; j < CircleXPostion.GetLength(1); j++) {
if (CircleXPostion[i, j] == 2) {
count++;
}
}
if (count == 3) {
playerWon = 2;
return;
}
}
//horizontal
//circle
for (int i = 0; i < CircleXPostion.GetLength(0); i++) {
int count = 0;
for (int j = 0; j < CircleXPostion.GetLength(1); j++) {
if (CircleXPostion[j, i] == 1) {
count++;
}
}
if (count == 3) {
playerWon = 1;
return;
}
}
//x
for (int i = 0; i < CircleXPostion.GetLength(0); i++) {
int count = 0;
for (int j = 0; j < CircleXPostion.GetLength(1); j++) {
if (CircleXPostion[j, i] == 2) {
count++;
2022-09-26 14:24:24 +02:00
//System.Console.WriteLine(count);
2022-09-23 11:27:45 +02:00
}
}
if (count == 3) {
playerWon = 2;
return;
}
}
//diagonal
//circle
2022-09-26 14:24:24 +02:00
int count2 = 0;
2022-09-23 11:27:45 +02:00
for (int i = 0; i < CircleXPostion.GetLength(0); i++) {
2022-09-26 14:24:24 +02:00
//System.Console.WriteLine("ii: " + i + "" + i);
2022-09-23 11:27:45 +02:00
if (CircleXPostion[i, i] == 1) {
2022-09-26 14:24:24 +02:00
count2++;
2022-09-26 16:18:34 +02:00
//System.Console.WriteLine(count2 + " kor");
2022-09-23 11:27:45 +02:00
}
2022-09-26 14:24:24 +02:00
if (count2 == 3) {
playerWon = 1;
2022-09-23 11:27:45 +02:00
return;
}
}
//x
2022-09-26 14:24:24 +02:00
int count3 = 0;
2022-09-23 11:27:45 +02:00
for (int i = 0; i < CircleXPostion.GetLength(0); i++) {
2022-09-26 14:24:24 +02:00
//System.Console.WriteLine("ii: " + i + "" + i);
2022-09-23 11:27:45 +02:00
if (CircleXPostion[i, i] == 2) {
2022-09-26 14:24:24 +02:00
count3++;
2022-09-26 16:18:34 +02:00
//System.Console.WriteLine(count3 + " X");
2022-09-23 11:27:45 +02:00
}
2022-09-26 14:24:24 +02:00
if (count3 == 3) {
2022-09-23 11:27:45 +02:00
playerWon = 2;
return;
}
}
//reverse diagonal
2022-09-27 11:21:57 +02:00
//circle
if (CircleXPostion[0,2] == 1 && CircleXPostion[1,1] == 1 && CircleXPostion[2,0] == 1) {
playerWon = 1;
return;
}else if(CircleXPostion[0, 2] == 2 && CircleXPostion[1, 1] == 2 && CircleXPostion[2, 0] == 2) {
playerWon = 2;
return;
}
2022-09-21 11:37:13 +02:00
}
}
}