Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lights Out #94

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/workflows/Lights Out Build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Lights Out Build
on:
push:
paths:
- 'Projects/Lights Out/**'
- '!**.md'
pull_request:
paths:
- 'Projects/Lights Out/**'
- '!**.md'
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x
- run: dotnet build "Projects\Lights Out\Lights Out.csproj" --configuration Release
10 changes: 10 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@
"console": "externalTerminal",
"stopAtEntry": false,
},
{
"name": "Lights Out",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "Build Lights Out",
"program": "${workspaceFolder}/Projects/Lights Out/bin/Debug/Lights Out.dll",
"cwd": "${workspaceFolder}/Projects/Lights Out/bin/Debug",
"console": "externalTerminal",
"stopAtEntry": false,
},
{
"name": "Wordle",
"type": "coreclr",
Expand Down
13 changes: 13 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,19 @@
],
"problemMatcher": "$msCompile",
},
{
"label": "Build Lights Out",
"command": "dotnet",
"type": "process",
"args":
[
"build",
"${workspaceFolder}/Projects/Lights Out/Lights Out.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary",
],
"problemMatcher": "$msCompile",
},
{
"label": "Build Solution",
"command": "dotnet",
Expand Down
9 changes: 9 additions & 0 deletions Projects/Lights Out/Lights Out.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Lights_Out</RootNamespace>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
310 changes: 310 additions & 0 deletions Projects/Lights Out/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,310 @@
using System;
using System.Text;

const int Length = 5;
const bool O = true;
const bool X = false;

bool[][][] levels =
[
[ // 1
[X, X, X, X, X],
[X, X, X, X, X],
[O, X, O, X, O],
[X, X, X, X, X],
[X, X, X, X, X],
],
[ // 2
[O, X, O, X, O],
[O, X, O, X, O],
[X, X, X, X, X],
[O, X, O, X, O],
[O, X, O, X, O],
],
[ // 3
[X, O, X, O, X],
[O, O, X, O, O],
[O, O, X, O, O],
[O, O, X, O, O],
[X, O, X, O, X],
],
[ // 4
[X, X, X, X, X],
[O, O, X, O, O],
[X, X, X, X, X],
[O, X, X, X, O],
[O, O, X, O, O],
],
[ // 5
[O, O, O, O, X],
[O, O, O, X, O],
[O, O, O, X, O],
[X, X, X, O, O],
[O, O, X, O, O],
],
[ // 6
[X, X, X, X, X],
[X, X, X, X, X],
[O, X, O, X, O],
[O, X, O, X, O],
[X, O, O, O, X],
],
[ // 7
[O, O, O, O, X],
[O, X, X, X, O],
[O, X, X, X, O],
[O, X, X, X, O],
[O, O, O, O, X],
],
[ // 8
[X, X, X, X, X],
[X, X, O, X, X],
[X, O, X, O, X],
[O, X, O, X, O],
[X, O, X, O, X],
],
[ // 9
[X, O, X, O, X],
[O, O, O, O, O],
[X, O, O, O, X],
[X, O, X, O, O],
[O, O, O, X, X],
],
[ // 10
[X, O, O, O, X],
[X, O, O, O, X],
[X, O, O, O, X],
[X, X, X, X, X],
[X, X, X, X, X],
],
[ // 11
[O, X, O, X, O],
[O, X, O, X, O],
[O, X, O, X, O],
[O, X, O, X, O],
[X, O, O, O, X],
],
[ // 12
[O, O, O, O, O],
[X, O, X, O, X],
[O, O, X, O, O],
[X, O, O, O, X],
[X, O, X, O, X],
],
[ // 13
[X, X, X, O, X],
[X, X, O, X, O],
[X, O, X, O, X],
[O, X, O, X, X],
[X, O, X, X, X],
],
[ // 14
[X, X, X, X, X],
[X, X, X, X, X],
[X, O, X, X, X],
[X, O, X, X, X],
[X, O, X, X, X],
],
[ // 15
[X, X, X, X, X],
[X, O, X, X, X],
[X, X, X, X, X],
[X, O, X, X, X],
[X, X, X, X, X],
],
[ // 16
[O, X, X, X, X],
[O, X, X, X, X],
[O, X, X, X, X],
[O, X, X, X, X],
[O, O, O, O, O],
],
[ // 17
[X, X, X, X, X],
[X, X, X, X, X],
[X, X, O, X, X],
[X, O, O, O, X],
[O, O, O, O, O],
],
[ // 18
[X, X, O, X, X],
[X, O, X, O, X],
[O, X, O, X, O],
[X, O, X, O, X],
[X, X, O, X, X],
],
[ // 19
[O, X, O, X, O],
[X, X, X, X, X],
[O, X, O, X, O],
[X, X, X, X, X],
[O, X, O, X, O],
],
[ // 20
[X, X, X, X, X],
[X, X, X, X, X],
[O, X, X, X, O],
[X, X, X, X, X],
[X, X, X, X, X],
],
[ // 21
[X, O, O, O, O],
[X, O, X, X, X],
[X, O, O, O, X],
[X, O, X, X, X],
[X, O, X, X, X],
],
[ // 22
[X, O, O, O, X],
[O, X, X, X, O],
[O, X, X, X, O],
[O, X, X, X, O],
[X, O, O, O, X],
],
[ // 23
[X, X, X, X, X],
[X, X, X, X, X],
[X, X, O, O, O],
[X, X, O, O, X],
[X, X, O, X, X],
],
[ // 24
[X, X, X, X, X],
[X, X, X, X, X],
[O, X, X, X, O],
[O, O, O, O, O],
[X, O, X, X, O],
],
[ // 25
[O, X, X, X, X],
[O, O, X, X, X],
[O, O, O, X, X],
[O, O, O, O, X],
[X, O, O, O, O],
],
];

Console.OutputEncoding = Encoding.UTF8;
int level = 0;
bool[][] board =
[
[O, O, O, O, O],
[O, O, O, O, O],
[O, O, O, O, O],
[O, O, O, O, O],
[O, O, O, O, O],
];
SetBoard();
(int Left, int Top) cursor = (0, 0);
while (true)
{
RenderBoard();
Console.Write($"""

Turn off all the lights. Level {level + 1}.

Controls:
- arrow keys: move cursor
- enter: flip lights
- backspace: reset level
- escape: close game
""");
Console.SetCursorPosition(2 * cursor.Left, cursor.Top);
Console.CursorVisible = false;
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.LeftArrow: cursor.Left = cursor.Left <= 0 ? Length - 1 : cursor.Left - 1; break;
case ConsoleKey.RightArrow: cursor.Left = cursor.Left >= Length - 1 ? 0 : cursor.Left + 1; break;
case ConsoleKey.UpArrow: cursor.Top = cursor.Top <= 0 ? Length - 1 : cursor.Top - 1; break;
case ConsoleKey.DownArrow: cursor.Top = cursor.Top >= Length - 1 ? 0 : cursor.Top + 1; break;
case ConsoleKey.Backspace: SetBoard(); break;
case ConsoleKey.Escape: goto Close;
case ConsoleKey.Enter:
board[cursor.Top][cursor.Left] = !board[cursor.Top][cursor.Left];
if (cursor.Top > 0) board[cursor.Top - 1][cursor.Left] = !board[cursor.Top - 1][cursor.Left];
if (cursor.Top < Length - 1) board[cursor.Top + 1][cursor.Left] = !board[cursor.Top + 1][cursor.Left];
if (cursor.Left > 0) board[cursor.Top][cursor.Left - 1] = !board[cursor.Top][cursor.Left - 1];
if (cursor.Left < Length - 1) board[cursor.Top][cursor.Left + 1] = !board[cursor.Top][cursor.Left + 1];
if (LightsOut())
{
Console.Clear();
RenderBoard();
Console.Write($"""

You turned off all the lights!
Level {level + 1} complete.

Controls:
- enter: next level
- escape: close game
""");
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.Escape: goto Close;
case ConsoleKey.Enter:
level++;
SetBoard();
Console.Clear();
break;
}
}
break;
}
}
Close:
Console.Clear();
Console.WriteLine("Lights Out was closed.");
Console.CursorVisible = true;

void SetBoard()
{
for (int i = 0; i < Length; i++)
{
for (int j = 0; j < Length; j++)
{
board[i][j] = levels[level][i][j];
}
}
}

bool LightsOut()
{
for (int i = 0; i < Length; i++)
{
for (int j = 0; j < Length; j++)
{
if (board[i][j])
{
return false;
}
}
}
return true;
}

void RenderBoard()
{
StringBuilder render = new();
for (int i = 0; i < Length; i++)
{
for (int j = 0; j < 3; j++)
{
for (int k = 0; k < Length; k++)
{
switch (j)
{
case 0: render.Append((k, i) == cursor ? "╔═╤═╗" : "╭───╮"); break;
case 1:
render.Append((k, i) == cursor ? '╟' : '│');
render.Append(board[i][k] ? "███" : " ");
render.Append((k, i) == cursor ? '╢' : '│');
break;
case 2: render.Append((k, i) == cursor ? "╚═╧═╝" : "╰───╯"); break;
}
}
render.AppendLine();
}
}
Console.SetCursorPosition(0, 0);
Console.Write(render);
}
Loading
Loading