Optimized the score method, fixed one major issue on it.

Still not working as it should
test_old_branch
Jules LASCRET 11 months ago
parent bfdc661165
commit 9e1b504451

@ -318,54 +318,119 @@ namespace QwirkleClassLibrary
return CheckTilesInLine(this.cellUsed, b, x, y) && surroundingCells.Any(cell => cell?.GetTile != null);
}
public int GetPlayerScore(Player player, List<Cell> cellsPlayed, Board board)
// public int GetPlayerScore(Player player, List<Cell> cellsPlayed, Board b)
// {
// // Compte le nombre de tuiles et ajoute ce nombre au score
// int score = cellsPlayed.Count;
//
// // Check les lignes / colonnes adjacentes aux tuiles placées
// for(int i= 0; i < cellsPlayed.Count; i++)
// {
// // A chaque tour, crée la liste des 4 cellules adjacentes à la cellule sélectionnée
// var surroundingCells = new[]
// {
// b.GetCell(cellsPlayed[i].GetX + 1, cellsPlayed[i].GetY),
// b.GetCell(cellsPlayed[i].GetX - 1, cellsPlayed[i].GetY),
// b.GetCell(cellsPlayed[i].GetX, cellsPlayed[i].GetY + 1),
// b.GetCell(cellsPlayed[i].GetX, cellsPlayed[i].GetY - 1)
// };
//
// // Parcourt le tableau dans la direction par rapport à ces cellules adjacentes
// foreach (var cell in surroundingCells)
// {
// // Si la cellule adjacente ne contient pas de tuile, on passe à la cellule adjacente suivante
// if (cell?.GetTile == null) break;
//
// if (cellsPlayed.Contains(cell) != true)
// {
//
// var dx = cell.GetX - cellsPlayed[i].GetX;
// var dy = cell.GetY - cellsPlayed[i].GetY;
//
// for (int j = 1; j < b.Rows; j++)
// {
// var extendedCell = b.GetCell(cellsPlayed[i].GetX + j * dx, cellsPlayed[i].GetY + j * dy);
//
// if (extendedCell?.GetTile == null)
// {
// break;
// }
//
// score += 1;
//
// }
// }
// }
//
// }
//
// ScoreBoard.Add(player, score);
//
// return score;
// }
public int GetPlayerScore(Player player, ReadOnlyCollection<Cell> cellsPlayed, Board b)
{
// Compte le nombre de tuiles et ajoute ce nombre au score
int score = cellsPlayed.Count;
// Check les lignes / colonnes adjacentes aux tuiles placées
for(int i= 0; i < cellsPlayed.Count; i++)
foreach (var cell in cellsPlayed)
{
// A chaque tour, crée la liste des 4 cellules adjacentes à la cellule sélectionnée
var surroundingCells = new[]
{
board.GetCell(cellsPlayed[i].GetX + 1, cellsPlayed[i].GetY),
board.GetCell(cellsPlayed[i].GetX - 1, cellsPlayed[i].GetY),
board.GetCell(cellsPlayed[i].GetX, cellsPlayed[i].GetY + 1),
board.GetCell(cellsPlayed[i].GetX, cellsPlayed[i].GetY - 1)
};
// Parcourt le tableau dans la direction par rapport à ces cellules adjacentes
foreach (var cell in surroundingCells)
score += CalculateAdjacentScore(cell, b, cellsPlayed);
}
if(ScoreBoard.TryAdd(player, score) == false)
{
ScoreBoard[player] += score;
}
return score;
}
private int CalculateAdjacentScore(Cell cell, Board b, ReadOnlyCollection<Cell> cellsPlayed)
{
int score = 0;
var surroundingCells = new[]
{
b.GetCell(cell.GetX + 1, cell.GetY),
b.GetCell(cell.GetX - 1, cell.GetY),
b.GetCell(cell.GetX, cell.GetY + 1),
b.GetCell(cell.GetX, cell.GetY - 1)
};
foreach (var adjacentCell in surroundingCells)
{
if (adjacentCell?.GetTile == null || cellsPlayed.Contains(adjacentCell))
{
// Si la cellule adjacente ne contient pas de tuile, on passe à la cellule adjacente suivante
if (cell?.GetTile == null) break;
continue;
}
else if (cellsPlayed.Contains(cell) != true)
{
int dx = adjacentCell.GetX - cell.GetX;
int dy = adjacentCell.GetY - cell.GetY;
var dx = cell.GetX - cellsPlayed[i].GetX;
var dy = cell.GetY - cellsPlayed[i].GetY;
score += CalculateLineScore(cell, dx, dy, b);
}
for (int j = 1; j < board.Rows; j++)
{
var extendedCell = board.GetCell(cellsPlayed[i].GetX + j * dx, cellsPlayed[i].GetY + j * dy);
return score;
}
if (extendedCell?.GetTile == null)
{
break;
}
private int CalculateLineScore(Cell cell, int dx, int dy, Board b)
{
int score = 0;
score += 1;
for (int i = 1; i < b.Rows; i++)
{
var extendedCell = b.GetCell(cell.GetX + i * dx, cell.GetY + i * dy);
}
}
if (extendedCell?.GetTile == null)
{
break;
}
score++;
}
ScoreBoard.Add(player, score);
return score;
}

@ -1,3 +1,5 @@
using System.Collections.ObjectModel;
namespace QwirkleClassLibrary;
public interface IPlayer
@ -12,5 +14,5 @@ public interface IPlayer
public bool SwapTiles(Player player, List<Tile> tilesToSwap);
public int GetPlayerScore(Player player, List<Cell> cellsPlayed, Board board);
public int GetPlayerScore(Player player, ReadOnlyCollection<Cell> cellsPlayed, Board b);
}

@ -136,7 +136,7 @@ static void MenuSwitch(Game game)
WriteLine("[1] Place your tiles");
WriteLine("[2] Swap your tiles");
WriteLine("[3] Skip your turn");
WriteLine("[3] End your turn / Skip your turn");
enter = Convert.ToInt32(ReadLine());
@ -150,6 +150,7 @@ static void MenuSwitch(Game game)
enter = 3;
break;
case 3:
WriteLine("Your score on this turn : " + game.GetPlayerScore(game.GetPlayingPlayer(), game.CellsUsed, game.GetBoard()));
game.EmptyCellUsed();
return;
}

Loading…
Cancel
Save