This commit is contained in:
bwbl 2024-10-03 16:23:08 +02:00
commit af356f294e
32 changed files with 582 additions and 0 deletions

13
.idea/.idea.p4.dir/.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,13 @@
# Default ignored files
/shelf/
/workspace.xml
# Rider ignored files
/.idea.p4.iml
/modules.xml
/projectSettingsUpdater.xml
/contentModel.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

4
.idea/.idea.p4.dir/.idea/encodings.xml generated Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
</project>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="UserContentModel">
<attachedFolders />
<explicitIncludes />
<explicitExcludes />
</component>
</project>

279
Program.cs Normal file
View File

@ -0,0 +1,279 @@
namespace P4
{
public enum Token
{
None,
FirstPlayer,
SecondPlayer,
}
public class Program
{
public void SetColor(ConsoleColor backgroundColor = ConsoleColor.Black, ConsoleColor forgroundColor = ConsoleColor.White)
{
Console.ForegroundColor = forgroundColor;
Console.BackgroundColor = backgroundColor;
}
static void Main(string[] args)
{
new Program().SetColor();
int[] boardSize = new Board().BoardSize();
Token[,] board = new Token[boardSize[0], boardSize[1]];
new Game().Start(board);
}
}
public class Game
{
private Draw d = new Draw();
public void Start(Token[,] board)
{
int pos = 0;
Token currentPlayer = Token.FirstPlayer;
bool placed = false;
while (!CheckWin(board))
{
if (CheckDraw(board))
break;
d.DrawBoard(board);
d.DrawPlayGround(board, currentPlayer, pos);
ConsoleKey key = Console.ReadKey().Key;
switch (key)
{
case ConsoleKey.RightArrow:
pos++;
break;
case ConsoleKey.LeftArrow:
pos--;
break;
case ConsoleKey.Enter:
case ConsoleKey.Spacebar:
placed = PlaceToken(board, currentPlayer, pos);
if (placed && currentPlayer == Token.FirstPlayer)
currentPlayer = Token.SecondPlayer;
else if (placed && currentPlayer == Token.SecondPlayer)
currentPlayer = Token.FirstPlayer;
break;
}
}
d.DrawBoard(board);
d.DrawPlayGround(board, currentPlayer, pos);
Console.Clear();
// currentplayer est inversé
if (CheckDraw(board))
Console.WriteLine("il y a eu une égalité");
else if (currentPlayer == Token.FirstPlayer)
Console.WriteLine("Le joueur " + "2" + " a gagné");
else if (currentPlayer == Token.SecondPlayer)
Console.WriteLine("Le joueur " + "1" + " a gagné");
Console.WriteLine("La partie à été gagnée en " + Turn(board) + " tours");
}
static int Turn(Token[,] board)
{
int count = 0; // compteur
for (int y = 0; y < board.GetLength(1); y++)
{
for (int x = 0; x < board.GetLength(0); x++)
{
if (board[x, y] != 0)
{
count++;
}
}
}
return count;
}
private bool CheckDraw(Token[,] board)
{
for (int y = 0; y < board.GetLength(1); y++)
{
for (int x = 0; x < board.GetLength(0); x++)
{
if (board[x, y] == 0)
{
return false;
}
}
}
return true;
}
private bool CheckWin(Token[,] board)
{
Token[] players = {Token.FirstPlayer, Token.SecondPlayer};
// boucle qui alterne entre les joueurs 1 et 2
foreach (var j in players)
{
// boucle qui fait toutes les colonnes
for (int y = 0; y < board.GetLength(1); y++)
{
// boucle qui fait toute la valeur des colonnes
for (int x = 0; x < board.GetLength(0); x++)
{
// on check les bordures horizontales
if (x + 3 < board.GetLength(0))
{
// on check les 4 prochains jetons dans le sens horizontal
if (board[x, y] == j &&
board[x + 1, y] == j &&
board[x + 2, y] == j &&
board[x + 3, y] == j)
{
// on retourne vrai si la condition est respectée
return true;
}
}
// on check les bordure verticales
if (y + 3 < board.GetLength(1))
{
// on check les 4 prochains jetons dans le sens vertical
if (board[x, y] == j &&
board[x, y + 1] == j &&
board[x, y + 2] == j &&
board[x, y + 3] == j)
{
// on retourne vrai si la condition est respectée
return true;
}
}
// on check les bordures horizontales et verticales
if (y + 3 < board.GetLength(1) && x + 3 < board.GetLength(0))
{
// on check les 4 prochains jetons dans le sens horizontal et vertical
if (board[x, y] == j &&
board[x + 1, y + 1] == j &&
board[x + 2, y + 2] == j &&
board[x + 3, y + 3] == j)
{
// on retourne vrai si la condition est respectée
return true;
}
}
// on check les bordures horizontales et verticales
if (y - 3 > 0 && x + 3 < board.GetLength(0))
{
// on check les 4 prochains jetons dans le sens horizontal et vertical
if (board[x, y] == j &&
board[x + 1, y - 1] == j &&
board[x + 2, y - 2] == j &&
board[x + 3, y - 3] == j)
{
// on retourne vrai si la condition est respectée
return true;
}
}
}
}
}
// si rien n'a été trouvé, on retourne false
return false;
}
private bool PlaceToken(Token[,] board, Token currentPlayer, int pos)
{
for (int i = board.GetLength(1) - 1; i >= 0; i--)
{
if (board[pos, i] != Token.None) continue;
board[pos, i] = currentPlayer;
return true;
}
return false;
}
}
public class Board
{
private Program p = new Program();
public int[] BoardSize()
{
int[] boardSize = { 0, 0 }; // initialisation de la taille
string[] input = { "", "" }; // initialisation de l'entrée utilisateur
// écriture des instructions en couleur grâce à la méthode WriteColor
Console.Write("La taille doit être comprise entre ");
p.SetColor(default, ConsoleColor.Red);
Console.Write("5x6");
p.SetColor();
Console.Write(" et ");
p.SetColor(default, ConsoleColor.Red);
Console.Write("13x16\n");
p.SetColor();
Console.WriteLine("exemple: 6x7 (6 lignes et 7 colonnes)");
Console.Write("taille: ");
// récupération de l'entrée utilisateur dans une liste grâce à un split
input = Console.ReadLine().Split('x');
// on essaye de Parse en int les entrées, sinon on fait une récursion
try
{
boardSize[0] = int.Parse(input[1]);
boardSize[1] = int.Parse(input[0]);
}
catch
{
Console.WriteLine("La taille est invalide. réessayer");
return BoardSize();
}
// on check si les entrées sont incorrectes si oui on fait une récursion
if (boardSize[1] <= 5 || boardSize[1] >= 13 || boardSize[0] <= 6 || boardSize[0] >= 16)
{
Console.WriteLine("La taille est invalide. réessayer");
return BoardSize();
}
// et si tous les checks sont passés, on retourne la taille
Console.WriteLine("taille valide");
return boardSize;
}
}
public class Draw
{
public void DrawPlayGround(Token[,] board, Token currentPlayer, int pos)
{
for (int x = 0; x < board.GetLength(0); x++)
{
Console.SetCursorPosition(x, 0);
if (pos == x) Console.Write(DrawToken(currentPlayer));
else Console.Write(DrawToken(Token.None));
}
}
public void DrawBoard(Token[,] board)
{
Console.Clear();
for (int y = 0; y < board.GetLength(1); y++)
for (int x = 0; x < board.GetLength(0); x++)
{
Console.SetCursorPosition(x, y + 2);
Console.Write(DrawToken(board[x, y]));
}
}
public char DrawToken(Token token)
{
switch (token)
{
case Token.FirstPlayer:
return 'X';
case Token.SecondPlayer:
return 'O';
case Token.None:
return '.';
default:
return default;
}
}
}
}

BIN
bin/Debug/net8.0/p4 Executable file

Binary file not shown.

View File

@ -0,0 +1,23 @@
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v8.0",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v8.0": {
"p4/1.0.0": {
"runtime": {
"p4.dll": {}
}
}
}
},
"libraries": {
"p4/1.0.0": {
"type": "project",
"serviceable": false,
"sha512": ""
}
}
}

BIN
bin/Debug/net8.0/p4.dll Normal file

Binary file not shown.

BIN
bin/Debug/net8.0/p4.pdb Normal file

Binary file not shown.

View File

@ -0,0 +1,12 @@
{
"runtimeOptions": {
"tfm": "net8.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "8.0.0"
},
"configProperties": {
"System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false
}
}
}

View File

@ -0,0 +1,4 @@
// <autogenerated />
using System;
using System.Reflection;
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v8.0", FrameworkDisplayName = ".NET 8.0")]

BIN
obj/Debug/net8.0/apphost Executable file

Binary file not shown.

View File

@ -0,0 +1,22 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("p4")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyProductAttribute("p4")]
[assembly: System.Reflection.AssemblyTitleAttribute("p4")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
// Generated by the MSBuild WriteCodeFragment class.

View File

@ -0,0 +1 @@
099e686f8384559c605ac543742bca589ca4af72f0a923dbfd812f36a5a55fe3

View File

@ -0,0 +1,13 @@
is_global = true
build_property.TargetFramework = net8.0
build_property.TargetPlatformMinVersion =
build_property.UsingMicrosoftNETSdkWeb =
build_property.ProjectTypeGuids =
build_property.InvariantGlobalization =
build_property.PlatformNeutralAssembly =
build_property.EnforceExtendedAnalyzerRules =
build_property._SupportedPlatformList = Linux,macOS,Windows
build_property.RootNamespace = p4
build_property.ProjectDir = /home/bwbl/dev/p4/
build_property.EnableComHosting =
build_property.EnableGeneratedComInterfaceComImportInterop =

View File

@ -0,0 +1,8 @@
// <auto-generated/>
global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
global using global::System.Net.Http;
global using global::System.Threading;
global using global::System.Threading.Tasks;

Binary file not shown.

View File

@ -0,0 +1 @@
e160bab6851984c3692690ecc0d3efc4d866e8c1041372f15e51c86751f15027

View File

@ -0,0 +1,14 @@
/home/bwbl/dev/p4/bin/Debug/net8.0/p4
/home/bwbl/dev/p4/bin/Debug/net8.0/p4.deps.json
/home/bwbl/dev/p4/bin/Debug/net8.0/p4.runtimeconfig.json
/home/bwbl/dev/p4/bin/Debug/net8.0/p4.dll
/home/bwbl/dev/p4/bin/Debug/net8.0/p4.pdb
/home/bwbl/dev/p4/obj/Debug/net8.0/p4.GeneratedMSBuildEditorConfig.editorconfig
/home/bwbl/dev/p4/obj/Debug/net8.0/p4.AssemblyInfoInputs.cache
/home/bwbl/dev/p4/obj/Debug/net8.0/p4.AssemblyInfo.cs
/home/bwbl/dev/p4/obj/Debug/net8.0/p4.csproj.CoreCompileInputs.cache
/home/bwbl/dev/p4/obj/Debug/net8.0/p4.dll
/home/bwbl/dev/p4/obj/Debug/net8.0/refint/p4.dll
/home/bwbl/dev/p4/obj/Debug/net8.0/p4.pdb
/home/bwbl/dev/p4/obj/Debug/net8.0/p4.genruntimeconfig.cache
/home/bwbl/dev/p4/obj/Debug/net8.0/ref/p4.dll

BIN
obj/Debug/net8.0/p4.dll Normal file

Binary file not shown.

View File

@ -0,0 +1 @@
226c357127cb10a4bba360c304edbad1ef2fdd0422f2f4cd9bab6f4a09802a11

BIN
obj/Debug/net8.0/p4.pdb Normal file

Binary file not shown.

BIN
obj/Debug/net8.0/ref/p4.dll Normal file

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,67 @@
{
"format": 1,
"restore": {
"/home/bwbl/dev/p4/p4.csproj": {}
},
"projects": {
"/home/bwbl/dev/p4/p4.csproj": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "/home/bwbl/dev/p4/p4.csproj",
"projectName": "p4",
"projectPath": "/home/bwbl/dev/p4/p4.csproj",
"packagesPath": "/home/bwbl/.nuget/packages/",
"outputPath": "/home/bwbl/dev/p4/obj/",
"projectStyle": "PackageReference",
"configFilePaths": [
"/home/bwbl/.nuget/NuGet/NuGet.Config"
],
"originalTargetFrameworks": [
"net8.0"
],
"sources": {
"https://api.nuget.org/v3/index.json": {}
},
"frameworks": {
"net8.0": {
"targetAlias": "net8.0",
"projectReferences": {}
}
},
"warningProperties": {
"warnAsError": [
"NU1605"
]
}
},
"frameworks": {
"net8.0": {
"targetAlias": "net8.0",
"imports": [
"net461",
"net462",
"net47",
"net471",
"net472",
"net48",
"net481"
],
"assetTargetFallback": true,
"warn": true,
"downloadDependencies": [
{
"name": "Microsoft.AspNetCore.App.Ref",
"version": "[8.0.8, 8.0.8]"
}
],
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "/usr/share/dotnet/sdk/8.0.108/PortableRuntimeIdentifierGraph.json"
}
}
}
}
}

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<RestoreSuccess Condition=" '$(RestoreSuccess)' == '' ">True</RestoreSuccess>
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">/home/bwbl/.nuget/packages/</NuGetPackageRoot>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">/home/bwbl/.nuget/packages/</NuGetPackageFolders>
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.8.1</NuGetToolVersion>
</PropertyGroup>
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<SourceRoot Include="/home/bwbl/.nuget/packages/" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" />

72
obj/project.assets.json Normal file
View File

@ -0,0 +1,72 @@
{
"version": 3,
"targets": {
"net8.0": {}
},
"libraries": {},
"projectFileDependencyGroups": {
"net8.0": []
},
"packageFolders": {
"/home/bwbl/.nuget/packages/": {}
},
"project": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "/home/bwbl/dev/p4/p4.csproj",
"projectName": "p4",
"projectPath": "/home/bwbl/dev/p4/p4.csproj",
"packagesPath": "/home/bwbl/.nuget/packages/",
"outputPath": "/home/bwbl/dev/p4/obj/",
"projectStyle": "PackageReference",
"configFilePaths": [
"/home/bwbl/.nuget/NuGet/NuGet.Config"
],
"originalTargetFrameworks": [
"net8.0"
],
"sources": {
"https://api.nuget.org/v3/index.json": {}
},
"frameworks": {
"net8.0": {
"targetAlias": "net8.0",
"projectReferences": {}
}
},
"warningProperties": {
"warnAsError": [
"NU1605"
]
}
},
"frameworks": {
"net8.0": {
"targetAlias": "net8.0",
"imports": [
"net461",
"net462",
"net47",
"net471",
"net472",
"net48",
"net481"
],
"assetTargetFallback": true,
"warn": true,
"downloadDependencies": [
{
"name": "Microsoft.AspNetCore.App.Ref",
"version": "[8.0.8, 8.0.8]"
}
],
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "/usr/share/dotnet/sdk/8.0.108/PortableRuntimeIdentifierGraph.json"
}
}
}
}

10
obj/project.nuget.cache Normal file
View File

@ -0,0 +1,10 @@
{
"version": 2,
"dgSpecHash": "xFvuXS4zcLqNlKMp1yTcoRj9X+eAVXZo01nF0SFLPeAVHNa8IA8rwFbjwifQxatpXJwXW76OP65X1EknDsHFBA==",
"success": true,
"projectFilePath": "/home/bwbl/dev/p4/p4.csproj",
"expectedPackageFiles": [
"/home/bwbl/.nuget/packages/microsoft.aspnetcore.app.ref/8.0.8/microsoft.aspnetcore.app.ref.8.0.8.nupkg.sha512"
],
"logs": []
}

View File

@ -0,0 +1 @@
"restore":{"projectUniqueName":"/home/bwbl/dev/p4/p4.csproj","projectName":"p4","projectPath":"/home/bwbl/dev/p4/p4.csproj","outputPath":"/home/bwbl/dev/p4/obj/","projectStyle":"PackageReference","originalTargetFrameworks":["net8.0"],"sources":{"https://api.nuget.org/v3/index.json":{}},"frameworks":{"net8.0":{"targetAlias":"net8.0","projectReferences":{}}},"warningProperties":{"warnAsError":["NU1605"]}}"frameworks":{"net8.0":{"targetAlias":"net8.0","imports":["net461","net462","net47","net471","net472","net48","net481"],"assetTargetFallback":true,"warn":true,"downloadDependencies":[{"name":"Microsoft.AspNetCore.App.Ref","version":"[8.0.8, 8.0.8]"}],"frameworkReferences":{"Microsoft.NETCore.App":{"privateAssets":"all"}},"runtimeIdentifierGraphPath":"/usr/share/dotnet/sdk/8.0.108/PortableRuntimeIdentifierGraph.json"}}

View File

@ -0,0 +1 @@
17279431593999150

View File

@ -0,0 +1 @@
17279431593999150

10
p4.csproj Normal file
View File

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>