Made a different game with the basics, Torches of NIM

I was powering through lectures the other night, and with the provided basics I figured I could put together a game of Nim.

It’s nice little game where there’s a number of objects and you and another player take turns removing up to three of them with the goal being to be the person to remove the last one. similar to the number guesser there’s a trick to make the computer do it much better than a human could

there’s one spot where randomization would be welcome. but otherwise it was a fun little project.

In the one spot, I could use some amount of randomization, since in the code’s current state pressing enter and mashing 3 is the only way to win the game…

Code:

using UnityEngine;
using System.Collections;

public class TorchesOfNim : MonoBehaviour {
	// number of Torches in play
	int Torches;
	// Set Starting Torches.
	int MaxTorch = 24;
	// Area to Calculate move for AI
	int OppSpace;
	int OppRemoved;
	void Start () {
		print ("Welcome to the torches of Nim");
		GameStart();
	}
	
	// Update is called once per frame
	void Update () {
	
	if (Input.GetKeyDown(KeyCode.Return)){
		if (Torches == MaxTorch){
			print ("Opponent will go first");
			OppTurn ();
			}	
			if (Torches <= 0){
			GameStart ();
			}
		}
	if (Torches > 0){
			if (Input.GetKeyDown (KeyCode.Alpha1)){
				print (">One torch will be extinguished");
				Torches = (Torches - 1);
				TorchTell ();
				OppTurn ();
			
			}
			else if (Input.GetKeyDown (KeyCode.Alpha2)){	
				print (">Two torches will be extinguished");
				Torches = (Torches - 2);
				TorchTell ();
				OppTurn ();
			}
			else if (Input.GetKeyDown (KeyCode.Alpha3)){
				if (Torches >= 0){
				print (">Three torches will be extinguished");
				Torches = (Torches - 3);
				TorchTell ();
				OppTurn ();
				}
			}
		}
	}
	void GameStart () {
		
		print ("===============================================================");
		Torches = MaxTorch;
		print (Torches + " torches sit before you in the darkened room");
		print ("extinguish the last torch to win. use 1,2,3 keys. to extinguish");
		print ("opponent will do the same, press Enter to go second");
	}
	void TorchTell () {
	//Comon Function to tell player how many torches remain. also acts as victory condition check to see if the player has won.
		if (Torches > 0){
		print (Torches + " torches remain in the darkened room");
		}
		if (Torches <= 0){
		print("Congratulations! you extinguished the last torch! Press Return key to play again.");
		}
	}
	void OppTurn () {
	// Code to be run when it is the Opponent turn. should attempt to get to the key posistions, multiples of 4
		OppSpace = (Torches/4*4);
		OppRemoved = (Torches - OppSpace);
		
		if (Torches > 0) {
			if (OppRemoved > 0) {
				print ("Opponent Extinguished " + OppRemoved + " Torch(es)");
				Torches = OppSpace;
				if (Torches <= 0) {
				print ("You failed to extinguish the final torch. Press Return to play again");
				}
				else if (Torches > 0) {
				TorchTell ();
				}
			}
			
			else if (OppRemoved == 0) {
			//Random number generation 1-3 would be super helpful here.
				print ("Opponent Extinguished 1 Torch");
				Torches = (Torches -1);
				TorchTell ();
			}
		}
		
		
	}
		
}

Feels like I might have made a slight formatting Error, since the start function is sticking to the top like that. or is that what it’s supposed to do?

1 Like

Hi @Ekayne20,

Well done on making your own game, and so early on in the course! Nim was a game I was introduced to in primary school, I remember it well, fond memories.

I sorted your code formatting for you, the following is for future reference for you :slight_smile:


See also;

Privacy & Terms