틱택톡 룰
- 두 플레이어가 번갈아 가며 △와 ○를 배치 합니다
- 가로 세로 대각선에 같은 문자가 배치되면 해당 플레이어가 승리합니다.
간단 구현 과정
- 3 * 3 배열을 생성하고 초기값 ( " [ ] " ) 을 넣습니다. .
- 배열을 출력합니다.
- 홀수 짝수를 이용해 순서를 구분하고 [○] , [△] 중 현재 순서에 맞는 값을 저장합니다.
- x , y 좌표를 이용해 저장된 [○] , [△] 중 하나를 배열 위에 위치시킵니다.
- 특정 키를 눌렀을 때 연산을 실행합니다
- 방향키 : x 와 y 의 값을 ++ , -- 를 이용해 증감합니다
- 엔터 : 현재 위치해 있는 x, y값을 이용해 배열에 저장합니다.
설명
짝수와 홀수를 이용해 현재 누구 순서인지 value에 저장합니다. value 값에 저장된 값을 출력된 배열 위에 위치 시킵니다.
촐력된 배열의 각 인덱스에 정확하게 커서를 옮겨주기 위해 x * 6 , y * 2을 했습니다.
방향키를 누르면 x , y의 값이 1씩 증가 또는 감소 합니다. 이 때 x 와 y의 값은 배열의 인덱스 범위 내에서만 증감이 가능하게 하였습니다. Enter키를 누르면 현재 x , y의 값을 이용해 배열에 현재 value를 저장하고 turn을 1증가 시킵니다.
승리 조건 체크
반복문을 이용해 가로 또는 세로 줄의 값이 같아졌는지 확인하고 만약 같다면 widthCheck(가로) ,highCheck(세로)에 True를 저장 합니다. 두 개의 조건문을 이용해 대각선의 값이 같아졌는지 확인하고 만약 같다면 diagonal1 , diagonal2 에 true를 저장합니다. widthCheck, highCheck , diagonal1 , diagonal2 중 하나라도 true가 있다면 arryFeildCheck에 true를 저장하고 게임이 종료 됩니다.
문제
처음에는 키를 누를때마다 배열 출력 -> Console.Clear() -> 배열 출력 과정이 작동되게 구현하였습니다. Console.Clear() 의 속도가 조금 느려서 움직일 때마다 화면이 깜빡거리는 문제점이 발생했습니다. 이를 해결하기 위해 Console.Clear() 코드를 제거하고 배열 출력을 하기 전에 콘솔 커서의 위치를 콘솔의 첫 부분으로 위치시키는 코드를 추가하였습니다.
using System;
namespace Array
{
internal class Program
{
static void Main(string[] args)
{
// 3 x 3 배열 선언
string[,] array = new string[3, 3];
// 배열에 기본값 넣어주는 함수
PutArray(array);
ConsoleKeyInfo inputKey;
// 커서와 배열 인덱스에 사용 될 x,y 좌표
int x = 0;
int y = 0;
// 몇번째 턴인지
int turn = 1;
// 플레이어 이름과 O X 구분
string value = "";
bool widthCheck = false;
bool highCheck = false;
// 대각선 \
bool a = false;
// 대각선 /
bool b = false;
while (true)
{
CheckMap(array, ref widthCheck, ref highCheck, ref a, ref b);
if (widthCheck == true || highCheck == true || a == true || b == true)
{
Console.WriteLine($"{value} 승리");
break;
} else
{
if (turn == 10)
{
Console.WriteLine($"무승부");
break;
}
}
// turn 을 이용해 player 값 가져오기
// value 값
// 플레이어 1 = [△]
// 플레이어 2 = [○]
Player(turn,ref value);
// 배열 출력
PrintArray(array, value);
// 배열 각 인덱스에 정확하게 커서 이동
Console.SetCursorPosition(x * 6, y * 2);
Console.Write($"{value}");
inputKey = Console.ReadKey(true);
Console.Clear();
switch (inputKey.Key)
{
case ConsoleKey.Enter:
if (array[y, x] == "[ ]")
{
array[y, x] = value;
turn++;
}
break;
case ConsoleKey.LeftArrow:
x--;
break;
case ConsoleKey.RightArrow:
if (x < array.GetLength(1) - 1)
{
x++;
}
break;
case ConsoleKey.UpArrow:
y--;
break;
case ConsoleKey.DownArrow:
if (y < array.GetLength(0) - 1)
{
y++;
}
break;
}
if (x < 0)
{
x = 0;
}
else if (y < 0)
{
y = 0;
}
}
}
static void Player(int turn, ref string value)
{
if (turn % 2 == 1)
{
value = "[△]";
}
else if (turn % 2 == 0)
{
value = "[○]";
}
}
static void CheckMap(string[,] mapArr, ref bool widthCheck, ref bool highCheck, ref bool a, ref bool b)
{
for (int i = 0; i < mapArr.GetLength(0); i++)
{
if (mapArr[i, 0] == mapArr[i, 1] && mapArr[i, 1] == mapArr[i, 2] && mapArr[i, 0] != "[ ]")
{
widthCheck = true;
}
else if (mapArr[0, i] == mapArr[1, i] && mapArr[1, i] == mapArr[2, i] && mapArr[0, i] != "[ ]")
{
highCheck = true;
}
}
if (mapArr[0, 0] == mapArr[1, 1] && mapArr[1, 1] == mapArr[2, 2] && mapArr[0, 0] != "[ ]")
{
a = true;
}
else if (mapArr[2, 0] == mapArr[1, 1] && mapArr[1, 1] == mapArr[0, 2] && mapArr[2, 0] != "[ ]")
{
b = true;
}
}
static void PrintArray(string[,] array, string value)
{
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
Console.Write(array[i, j]);
Console.Write(" ");
}
Console.WriteLine("");
Console.WriteLine("");
}
Console.WriteLine($"\n{value} 차례입니다 \n\n");
Console.WriteLine("틱택토 룰 \n");
Console.WriteLine("1. 두 플레이어가 번갈아 가며 △와 ○를 배치 합니다\n");
Console.WriteLine("2. 가로 세로 대각선에 같은 문자가 배치되면 해당 플레이어가 승리합니다.\n");
}
static void PutArray(string[,] array)
{
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
array[i, j] = "[ ]";
}
}
}
}
}
https://www.youtube.com/watch?v=D2k2KllK4Fo
좌표로
using System;
using System.Collections.Generic;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace Tiktato
{
internal class Program
{
static void Main(string[] args)
{
// 사용자 입력값 저장 변수
int playerInputX = 0;
int playerInputY = 0;
// 가로 세로 체크
bool widthCheck = false;
bool highCheck = false;
bool a = false;
bool b = false;
// 플레이어 1,2 구분 (짝수,홀수)
int gameTurn = 1;
string playerTurn = "1";
string playerTurnCol = "0";
string[,] mapArr = new string[3, 3];
FirstArray(mapArr);
while (gameTurn < 10)
{
TurnPlayer(gameTurn, ref playerTurn, ref playerTurnCol);
PrintArray(mapArr);
Repeat(mapArr, ref playerInputX, ref playerInputY, ref playerTurn, ref playerTurnCol);
if (mapArr[playerInputX, playerInputY] == "[ ]")
{
mapArr[playerInputX, playerInputY] = $"{playerTurnCol}";
}
else
{
Console.WriteLine("거긴 못둬");
continue;
}
CheckMap(mapArr, ref widthCheck, ref highCheck, ref a, ref b);
if (widthCheck == true || highCheck == true || a == true || b == true)
{
Console.Clear();
PrintArray(mapArr);
Console.WriteLine($"{playerTurn}승리");
break;
}
if (gameTurn > 10)
{
Console.WriteLine("무승부");
break;
}
gameTurn++;
Console.Clear();
}
//if (승리조건)
//{
// break;
//}
}
static void TurnPlayer(int gameTurn, ref string playerTurn, ref string playerTurnCol)
{
if (gameTurn % 2 == 1)
{
playerTurn = "플레이어1";
playerTurnCol = "[ O ]";
}
else if (gameTurn % 2 == 0)
{
playerTurn = "플레이어2";
playerTurnCol = "[ X ]";
}
}
static void CheckMap(string[,] mapArr, ref bool widthCheck, ref bool highCheck, ref bool a, ref bool b)
{
for (int i = 0; i < mapArr.GetLength(0); i++)
{
if (mapArr[i, 0] == mapArr[i, 1] && mapArr[i, 1] == mapArr[i, 2] && mapArr[i, 0] != "[ ]")
{
widthCheck = true;
}
else if (mapArr[0, i] == mapArr[1, i] && mapArr[1, i] == mapArr[2, i] && mapArr[0, i] != "[ ]")
{
highCheck = true;
}
}
if (mapArr[0, 0] == mapArr[1, 1] && mapArr[1, 1] == mapArr[2, 2] && mapArr[0, 0] != "[ ]")
{
a = true;
}
else if (mapArr[2, 0] == mapArr[1, 1] && mapArr[1, 1] == mapArr[0, 2] && mapArr[2, 0] != "[ ]")
{
b = true;
}
}
static void PrintArray(string[,] mapArr)
{
for (int i = 0; i < mapArr.GetLength(0); i++)
{
for (int j = 0; j < mapArr.GetLength(1); j++)
{
Console.Write(mapArr[i, j] + " ");
}
Console.WriteLine("");
Console.WriteLine("");
}
}
static void FirstArray(string[,] mapArr)
{
for (int i = 0; i < mapArr.GetLength(0); i++)
{
for (int j = 0; j < mapArr.GetLength(1); j++)
{
mapArr[i, j] = "[ ]";
}
}
}
static void Repeat(string[,] mapArr, ref int playerInputX, ref int playerInputY, ref string playerTurn, ref string playerTurnCol)
{
while (true)
{
Console.WriteLine($"{playerTurn} , {playerTurnCol}을 넣을 좌표를 입력해주세요.");
Console.Write($"x 좌표 ");
bool InputStrCheckX = int.TryParse(Console.ReadLine(), out playerInputX);
Console.Write($"y 좌표 ");
bool InputStrCheckY = int.TryParse(Console.ReadLine(), out playerInputY);
bool InputRangeCheckX = 0 <= playerInputX && playerInputX <= 2;
bool InputRangeCheckY = 0 <= playerInputY && playerInputY <= 2;
if (InputStrCheckX == true && InputStrCheckY == true && InputRangeCheckX == true && InputRangeCheckY == true)
{
break;
}
Console.WriteLine("잘못 입력되었습니다");
}
}
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ldjlajljasldjflskfjwon
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■");
ConsoleKeyInfo inputKey;
// 30
int consoleX = 1;
// 30
int consoleY = 1;
int hurt = 4;
Random random = new Random();
Stopwatch watch = new Stopwatch();
watch.Start();
long endScore = 100000000000000000;
long playerScore = 0;
while (playerScore < endScore)
{
// playerScore = (long)watch.ElapsedMilliseconds;
// Console.WriteLine(playerScore);
Console.SetCursorPosition(consoleX * 2, consoleY);
Console.Write("●");
inputKey = Console.ReadKey(true);
switch (inputKey.Key)
{
// -▶
case ConsoleKey.RightArrow:
consoleX++;
Console.SetCursorPosition((consoleX - 1) * 2, consoleY);
Console.Write("□");
break;
// ◀-
case ConsoleKey.LeftArrow:
consoleX--;
Console.SetCursorPosition((consoleX + 1) * 2, consoleY);
Console.Write("□");
break;
// ▲
// ㅣ
case ConsoleKey.UpArrow:
consoleY--;
Console.SetCursorPosition(consoleX * 2, consoleY + 1);
Console.Write("□");
break;
// ㅣ
// ▼
case ConsoleKey.DownArrow:
consoleY++;
Console.SetCursorPosition(consoleX * 2, consoleY - 1);
Console.Write("□");
break;
}
// 왼쪽 밖으로 못나가게
if (consoleX < 1)
{
consoleX = 1;
}
// 위쪽 밖으로 못나가게
else if (consoleY < 1)
{
consoleY = 1;
}
// 오른쪽 밖으로 못나가게
else if (consoleX > 28)
{
consoleX = 28;
}
// 아래쪽 밖으로 못나가게
else if (consoleY > 28)
{
consoleY = 28;
}
}
watch.Stop();
}
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ldjlajljasldjflskfjwon
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■□□□□□□□□□□□□□□□□□□□□□□□□□□□□■");
Console.WriteLine("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■");
ConsoleKeyInfo inputKey;
// 30
int consoleX = 1;
// 30
int consoleY = 1;
int hurt = 4;
Random random = new Random();
Stopwatch watch = new Stopwatch();
watch.Start();
long endScore = 100000000000000000;
long playerScore = 0;
watch.Start();
int aaX = 2;
int aaY = 1;
string gunChar = "▶";
bool isFired = false;
while (playerScore < endScore)
{
// playerScore = (long)watch.ElapsedMilliseconds;
// Console.WriteLine(playerScore);
Console.SetCursorPosition(consoleX * 2, consoleY);
Console.Write("●");
if (watch.ElapsedMilliseconds >= 160)
{
watch.Restart();
if (Console.KeyAvailable) //키가 눌렸을때만 참
{
inputKey = Console.ReadKey(true);
switch (inputKey.Key)
{
// -▶
case ConsoleKey.RightArrow:
isFired = true;
consoleX++;
Console.SetCursorPosition((consoleX - 1) * 2, consoleY);
Console.Write("□");
break;
// ◀-
case ConsoleKey.LeftArrow:
isFired = true;
consoleX--;
Console.SetCursorPosition((consoleX + 1) * 2, consoleY);
Console.Write("□");
break;
// ▲
// ㅣ
case ConsoleKey.UpArrow:
isFired = true;
consoleY--;
Console.SetCursorPosition(consoleX * 2, consoleY + 1);
Console.Write("□");
break;
// ㅣ
// ▼
case ConsoleKey.DownArrow:
isFired = true;
consoleY++;
Console.SetCursorPosition(consoleX * 2, consoleY - 1);
Console.Write("□");
break;
}
// 왼쪽 밖으로 못나가게
if (consoleX < 1)
{
consoleX = 1;
}
// 위쪽 밖으로 못나가게
else if (consoleY < 1)
{
consoleY = 1;
}
// 오른쪽 밖으로 못나가게
else if (consoleX > 28)
{
consoleX = 28;
}
// 아래쪽 밖으로 못나가게
else if (consoleY > 28)
{
consoleY = 28;
}
}
if (isFired == true)
{
Console.SetCursorPosition(aaX, aaY);
Console.WriteLine(" ");
aaX += 2;
Console.SetCursorPosition(aaX, aaY);
Console.Write(gunChar);
if (aaX > 54)
{
Console.SetCursorPosition(aaX, aaY);
Console.Write(" ");
break;
}
}
}
}
while (true)
{
}
}
}
}
using System;
using System.Diagnostics;
namespace Day11
{
internal class Program
{
struct Vector2
{
public int x;
public int y;
}
struct Bullet
{
public int posX;
public int posY;
public bool isFired;
}
static void Main(string[] args)
{
Console.CursorVisible = false; //커서 안보이게
ConsoleKeyInfo temp; //키입력 기억할 변수
Stopwatch watch = new Stopwatch(); //이 한줄 적으면 스탑워치가 만들어진다
watch.Start(); //스탑워치 시작함
Vector2 playerPos;
playerPos.x = 0;
playerPos.y = 0;
// 값 초기화
bool isFired = false;
int posX = 0;
int posY = 0;
while (true)
{
//=================입력만!!!=====================
if (Console.KeyAvailable) //키가 눌렸을때만 참
{
temp = Console.ReadKey(true); //키입력이 담김
if (temp.Key == ConsoleKey.Spacebar) //스페이스 바 눌리면 true로 바꾸기
{
if (isFired == true)
{
continue;
}
else
{
posX = playerPos.x;
posY = playerPos.y;
isFired = true;
}
}
else if (temp.Key == ConsoleKey.A)
{
playerPos.x -= 2;
}
else if (temp.Key == ConsoleKey.D)
{
playerPos.x += 2;
}
else if (temp.Key == ConsoleKey.W)
{
playerPos.y--;
}
else if (temp.Key == ConsoleKey.S)
{
playerPos.y++;
}
}
//===========================================로직 처리
if (watch.ElapsedMilliseconds >= 2000)
{
if (isFired == true)
{
posX += 2;
if (posX > 100)
{
isFired = false;
}
}
else
{
continue;
}
watch.Restart();
}
//==============================================그리는 부분==================
Console.SetCursorPosition(playerPos.x, playerPos.y);
Console.Write("◈");
// foreach (var a in bullets)
// {
Console.SetCursorPosition(posX,posY);
Console.Write("▶");
// }
}
}
}
}