changed function for shifting cells and marks. now allows for wrap around - next up is incorporating logic to delete or clear cells that get wrapped around

This commit is contained in:
2026-01-29 01:44:29 -05:00
parent f07139c440
commit d9d33f9758
4 changed files with 120 additions and 35 deletions

View File

@@ -54,6 +54,39 @@ public partial class Actor : Sprite2D
return true; return true;
} }
public bool RotateMarks(int ROTATIONS = 1)
{
List<Cell> cells = new(_board._cells);
List<int> addresses = new(_board._cells.Select(c=>c._address).ToList());
Dictionary<int, int> cwShifts = new()
{
{0,1}, {1,2}, {2,5}, {3,0}, {4,4}, {5,8}, {6,3}, {7,6}, {8,7}
};
Dictionary<int, int> ccwShifts = new()
{
{0,3}, {1,0}, {2,1}, {3,6}, {4,4}, {5,2}, {6,7}, {7,8}, {8,5}
};
Dictionary<int, int> shifts = ROTATIONS > 0 ? cwShifts : ccwShifts;
for (int i = 0; i < Math.Abs(ROTATIONS); i++)
{
for (int j = 0; j < addresses.Count; j++)
{
int address = addresses[j];
int newAddress = shifts[address];
addresses[j] = newAddress;
}
}
for (int i = 0; i < _board._cells.Count; i++)
{
int newCellAddress = addresses[i];
_board._cells[i].Mark(cells[newCellAddress]._marker);
}
return true;
}
public bool ShuffleCells() public bool ShuffleCells()
{ {
List<Vector2> positions = new(_board._cells.Select(c=>c.Position).ToList()); List<Vector2> positions = new(_board._cells.Select(c=>c.Position).ToList());
@@ -66,39 +99,63 @@ public partial class Actor : Sprite2D
return true; return true;
} }
public bool ShiftMarks(int COLUMNS = 0, int ROWS = 0) public bool ShuffleMarks()
{ {
List<Actor> markers = new(_board._cells.Select(c=>c._marker).ToList()); List<Cell> cells = new(_board._cells);
int[,] cellsToTable = {{0,1,2},{3,4,5},{6,7,8}}; Globals.Shuffle(cells);
for (int i = 0; i < cellsToTable.GetLength(0); i++) //ROWS / Y for (int i = 0; i < _board._cells.Count; i++)
{ {
for (int j = 0; j < cellsToTable.GetLength(1); j++) //COLUMNS / X _board._cells[i].Mark(cells[i]._marker);
{
int toY = ROWS >= 0 ? cellsToTable.GetLength(0) - 1 - i : i;
int fromY = toY - ROWS;
int toX = COLUMNS >= 0 ? cellsToTable.GetLength(1) - 1 - j : j;
int fromX = toX - COLUMNS;
if (fromY >= 0 && fromY <= cellsToTable.GetLength(0) && fromX >= 0 && fromX <= cellsToTable.GetLength(1))
{
int toI = cellsToTable[toY, toX];
int fromI = cellsToTable[fromY, fromX];
if (_board._cells[fromI]._marker != null)
{
_board._cells[toI].Mark(_board._cells[fromI]._marker);
_board._cells[fromI].Mark();
} }
else
{
_board._cells[toI].Mark();
}
}
}
}
_board.RenumberCells();
return true; return true;
} }
public bool ShiftCells(int SHIFT_ROWS_BY = 0,int SHIFT_COLUMNS_BY = 0)
{
List<Vector2> positions = new(_board._cells.Select(c=>c.Position).ToList());
int[,] addressTable = {{0,1,2},{3,4,5},{6,7,8}};
List<int> rows = new(){0,1,2};
List<int> columns = new(){0,1,2};
rows = Globals.ShiftList(rows, SHIFT_ROWS_BY);
columns = Globals.ShiftList(columns, SHIFT_COLUMNS_BY);
for (int i = 0; i < rows.Count; i++)
{
for (int j = 0; j < columns.Count; j++)
{
_board._cells[addressTable[rows[i],columns[j]]].Position = positions[addressTable[i,j]];
}
}
return true;
}
public bool ShiftMarks(int SHIFT_ROWS_BY = 0,int SHIFT_COLUMNS_BY = 0)
{
List<Actor> markers = new(_board._cells.Select(c=>c._marker).ToList());
int[,] addressTable = {{0,1,2},{3,4,5},{6,7,8}};
List<int> rows = new(){0,1,2};
List<int> columns = new(){0,1,2};
rows = Globals.ShiftList(rows, SHIFT_ROWS_BY);
columns = Globals.ShiftList(columns, SHIFT_COLUMNS_BY);
for (int i = 0; i < rows.Count; i++)
{
for (int j = 0; j < columns.Count; j++)
{
_board._cells[addressTable[rows[i],columns[j]]].Mark(markers[addressTable[i,j]]);
}
}
return true;
}
public bool SlideCells(int SHIFT_SPOTS = 1) public bool SlideCells(int SHIFT_SPOTS = 1)
{ {
List<Vector2> positions = new(_board._cells.Select(c=>c.Position).ToList()); List<Vector2> positions = new(_board._cells.Select(c=>c.Position).ToList());
@@ -112,6 +169,19 @@ public partial class Actor : Sprite2D
return true; return true;
} }
public bool SlideMarks(int SHIFT_SPOTS = 1)
{
List<Vector2> positions = new(_board._cells.Select(c=>c.Position).ToList());
int start = SHIFT_SPOTS > 0 ? 0 : 9;
for (int i = 0; i < _board._cells.Count; i++)
{
int newSpot = (start + i + SHIFT_SPOTS) % 9;
_board._cells[i].Position = positions[newSpot];
}
_board.RenumberCells();
return true;
}
public bool SwapCells(int CELL_A_ADDRESS = -1, int CELL_B_ADDRESS = -1) public bool SwapCells(int CELL_A_ADDRESS = -1, int CELL_B_ADDRESS = -1)
{ {
if (CELL_A_ADDRESS < -1 || CELL_A_ADDRESS > 8 || CELL_B_ADDRESS < -1 || CELL_B_ADDRESS > 8 || CELL_A_ADDRESS == CELL_B_ADDRESS) if (CELL_A_ADDRESS < -1 || CELL_A_ADDRESS > 8 || CELL_B_ADDRESS < -1 || CELL_B_ADDRESS > 8 || CELL_A_ADDRESS == CELL_B_ADDRESS)

View File

@@ -2,6 +2,6 @@ public partial class Shift1ColumnRight : Contact
{ {
public override void CallAction() public override void CallAction()
{ {
_player.ShiftMarks(1,0); _player.ShiftCells(0,1);
} }
} }

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Godot;
public static partial class Globals public static partial class Globals
{ {
@@ -7,15 +8,29 @@ public static partial class Globals
public static Random _rng = new(); public static Random _rng = new();
public static List<string> _addressTranslation = new(){"NW","N","NE","W","C","E","SW","S","SE"}; public static List<string> _addressTranslation = new(){"NW","N","NE","W","C","E","SW","S","SE"};
public static void Shuffle<T>(this IList<T> list) public static List<T> ShiftList<T>(List<T> LIST, int SHIFT_BY)
{ {
int n = list.Count; if (LIST.Count <= SHIFT_BY || LIST.Count == 0 || SHIFT_BY ==0)
{
return LIST;
}
int getRangeStart = SHIFT_BY < 0 ? -SHIFT_BY : LIST.Count - SHIFT_BY;
int getRangeEnd = SHIFT_BY < 0 ? LIST.Count + SHIFT_BY : SHIFT_BY;
int addRangeEnd = SHIFT_BY < 0 ? -SHIFT_BY : LIST.Count - SHIFT_BY;
var result = LIST.GetRange(getRangeStart, getRangeEnd);
result.AddRange(LIST.GetRange(0, addRangeEnd));
return result;
}
public static void Shuffle<T>(IList<T> LIST)
{
int n = LIST.Count;
while (n > 1) { while (n > 1) {
n--; n--;
int k = _rng.Next(n + 1); int k = _rng.Next(n + 1);
T value = list[k]; T value = LIST[k];
list[k] = list[n]; LIST[k] = LIST[n];
list[n] = value; LIST[n] = value;
} }
} }

View File

@@ -1,9 +1,9 @@
[gd_scene load_steps=3 format=3 uid="uid://0vj01cjcpibt"] [gd_scene format=3 uid="uid://0vj01cjcpibt"]
[ext_resource type="Script" uid="uid://pg7mpir3ewhw" path="res://Gameplay/Cell.cs" id="1_lehgd"] [ext_resource type="Script" uid="uid://pg7mpir3ewhw" path="res://Gameplay/Cell.cs" id="1_lehgd"]
[ext_resource type="Texture2D" uid="uid://g6ikqlh8yccy" path="res://Art/blanksquare.jpg" id="2_mxyjk"] [ext_resource type="Texture2D" uid="uid://g6ikqlh8yccy" path="res://Art/blanksquare.jpg" id="2_mxyjk"]
[node name="Cell" type="TextureButton"] [node name="Cell" type="TextureButton" unique_id=1408990258]
modulate = Color(1, 1, 1, 0.2) modulate = Color(1, 1, 1, 0.2)
offset_right = 200.0 offset_right = 200.0
offset_bottom = 200.0 offset_bottom = 200.0
@@ -14,6 +14,6 @@ texture_disabled = ExtResource("2_mxyjk")
texture_focused = ExtResource("2_mxyjk") texture_focused = ExtResource("2_mxyjk")
script = ExtResource("1_lehgd") script = ExtResource("1_lehgd")
[node name="DefaultMark" type="Sprite2D" parent="."] [node name="DefaultMark" type="Sprite2D" parent="." unique_id=1684344431]
visible = false visible = false
texture = ExtResource("2_mxyjk") texture = ExtResource("2_mxyjk")