Skip to content

Instantly share code, notes, and snippets.

View MylesColeman's full-sized avatar

MylesColeman

  • 00:48 (UTC +01:00)
View GitHub Profile
@MylesColeman
MylesColeman / main.cpp
Created October 22, 2025 18:08
Maze game, maze save function
void saveMaze(string fileName)
{
cout << "Saving - Enter File Name" << endl;
cin >> fileName;
output.open(fileName+".txt");
// Iterates through the maze saving each character to a file
for (int y = 0; y < kMazeRowsY; y++)
{
@MylesColeman
MylesColeman / Circle.cpp
Created October 22, 2025 18:05
Circle constructor
// Defines a circle
Circle::Circle(int gScreenWidth, int gScreenHeight, int speed)
{
moveCircleX = rand() % gScreenWidth / 2 + gScreenWidth / 4;
moveCircleY = rand() % gScreenHeight / 2 + gScreenWidth / 4;
radius = rand() % 20 + 5;
xDir = rand() % speed - 5;
yDir = rand() % speed - 5;
@MylesColeman
MylesColeman / LinkedList.java
Created October 22, 2025 16:14
Delete function for a linked list.
// precondition: list is pointed to by head; last node, if any, points to null
// postcondition: nth node is deleted if it exists. A 1-based index is used
public boolean delete(int n)
{
if (head == null)
return false;
if (n == 1)
{
ListNode temp = head;
@MylesColeman
MylesColeman / D_LinkedList.java
Last active October 22, 2025 16:13
Add after function for a doubly linked list.
// postcondition: Adds a node storing obj after position index in list.
public boolean addAfterPos(int obj, int index)
{
if (head == null)
return false;
if (index < 1)
return false;
D_ListNode newNode = new D_ListNode(obj);
@MylesColeman
MylesColeman / C_LinkedList.java
Created October 22, 2025 16:08
Add first function for a circular linked list.
// postcondition: Adds a node storing obj at front of this list.
public void addFirst(int obj)
{
ListNode newNode = new ListNode(obj);
if (head.next == head)
{
newNode.next = head;
head.next = newNode;
}
@MylesColeman
MylesColeman / gist:88bdf5e0e018a97523e464297d6dd97c
Created October 21, 2025 21:31
Input Manager for my Snake Game
#include "InputManager.h"
void InputManager::AddListener(IReceivesInput* listener)
{
m_listeners.push_back(listener);
}
void InputManager::ClearListeners()
{
m_listeners.clear();
@MylesColeman
MylesColeman / AISnake.cpp
Created October 21, 2025 21:15
AI Snake Reward System
void AISnake::Update(GameData& m_gameData)
{
FindClosestFood(m_gameData);
// Adds all directions to vector to determine the best one
std::vector<std::pair<float, Direction>> bestDirections;
bestDirections.push_back({ CalculateRewardForDirection(Direction::Up, m_gameData), Direction::Up });
bestDirections.push_back({ CalculateRewardForDirection(Direction::Left, m_gameData), Direction::Left });
bestDirections.push_back({ CalculateRewardForDirection(Direction::Down, m_gameData), Direction::Down });
bestDirections.push_back({ CalculateRewardForDirection(Direction::Right, m_gameData), Direction::Right });
@MylesColeman
MylesColeman / Linked List.h
Created October 21, 2025 21:11
Linked List Implementation for my game of Snake
#pragma once
#include <SFML/Graphics.hpp>
#include <iostream>
template <typename T>
class Node
{
public:
Node(T newData) : data{ newData } {}
@MylesColeman
MylesColeman / Connect Four.cpp
Last active October 21, 2025 21:14
Connect Four Input Validation
// Ensures the input is within bounds, in a free column, or input is numerical
bool validInput = false;
while (!validInput)
{
std::cin >> chosenColumn;
if (chosenColumn < 1 || chosenColumn > 7 || std::cin.fail())
{
std::cin.clear();
std::cin.ignore(10000, '\n'); // Resets the cin, either by ignoring 10,000 characters or when a new line is hit
@MylesColeman
MylesColeman / Connect Four.cpp
Last active October 21, 2025 21:13
Connect Four Winner Detection
// Checks for a winner by looping through the whole board
bool checkForWinner(const std::vector<std::vector<char>>& board, char playerPiece)
{
// Horizontal Win
for (int r = 0; r < rows; r++)
{
for (int c = 0; c <= cols - 4; c++) // Don't need to check last few columns, as they're impossible to win from. As we're checking from left to right - would exit bounds
{
if (board[r][c] == playerPiece &&
board[r][c + 1] == playerPiece &&