Archives from S:\ Drive

Good to keep around methinks
This commit is contained in:
Amrit Panesar committed 2015-04-01 21:23:15 -07:00
1 parent eadaf70b9e
commit 6dd63cc439
606 files changed
+50212

No files matched your search

@@ -0,0 +1,20 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TicTacToe", "TicTacToe\TicTacToe.vcxproj", "{24EA466B-50AD-4F6D-85B6-FE99AA7ADC3E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Release|Win32 = Release|Win32
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{24EA466B-50AD-4F6D-85B6-FE99AA7ADC3E}.Debug|Win32.ActiveCfg = Debug|Win32
{24EA466B-50AD-4F6D-85B6-FE99AA7ADC3E}.Debug|Win32.Build.0 = Debug|Win32
{24EA466B-50AD-4F6D-85B6-FE99AA7ADC3E}.Release|Win32.ActiveCfg = Release|Win32
{24EA466B-50AD-4F6D-85B6-FE99AA7ADC3E}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

@@ -0,0 +1,148 @@
/******************************************************************************
* Project Two: Tic-Tac-Toe *
* Author: Amrit Panesar *
* Date: 08/20/2014 *
* License: Released into the Public Domain *
*****************************************************************************/
#include "includes.h"
#include "main.h"
int _tmain(int argc, _TCHAR* argv[])
{
ConsoleSetup("Tic-Tac-Toe");
string name = "";
while (name.length() == 0)
{
cout << "Please enter your name" << endl << ": ";
cin >> name;
bool breakin = false;
while (!breakin)
{
cout << "Your name is: "<< name << endl << "Is this correct? " << endl << "[Y/n]: ";
int in = _getch();
cout << (char)in << endl;
switch (toupper(in))
{
case 'Y':
breakin = true;
break;
case 'N':
breakin = true;
name = "";
break;
}
system("cls");
}
}
char player = ' ';
while (player == ' ')
{
cout << "Please enter one character for your board marker" << endl << ": ";
player = _getch();
cout << player << endl;
bool breakin = false;
while (!breakin)
{
cout << "Your marker is: "<< player << endl << "Is this correct? " << endl << "[Y/n]: ";
int in = _getch();
cout << (char)in << endl;
switch (toupper(in))
{
case 'Y':
breakin = true;
break;
case 'N':
breakin = true;
player = ' ';
break;
}
system("cls");
}
}
char computer = ' ';
if (player == 'X')
computer = 'O';
else
computer = 'X';
TicTacToe t = TicTacToe(name, player, computer);
while (true)
{
t.run();
cout << "Would you like to play again?" << endl << "[Y/n]: ";
char hold = toupper(_getch());
cout << hold << endl;
if (hold == 'Y' || hold == 'N' || hold == LF || hold == ESC)
{
if (hold == 'N' || hold == ESC)
break;
}
else
{
cout << "Please enter either Y or N." << endl;
continue;
}
}
cout << "Game Over." << endl << endl;
cout << "Press any key to continue..." << endl;
_getch();
}
void ConsoleSetup(char* input)
{
system("color 1e");
ChangeConsoleIcon(IDI_ICON1);
ChangeConsoleTitle(input);
cout << string(80,'*') << "Welcome to " << input << " " << GetYear() << " Edition!" << endl << string(80,'*') << endl << endl;
}
void ChangeConsoleTitle (const char* input)
{
time_t t = time(NULL);
tm* timePtr = localtime(&t);
TCHAR szNewTitle[MAX_PATH];
StringCchPrintf(szNewTitle, MAX_PATH, TEXT("%s %d"), input, GetYear());
SetConsoleTitle(szNewTitle);
}
void ChangeConsoleIcon (const int resource_number)
{
HMODULE hMainMod = GetModuleHandle(0);
assert (hMainMod);
HICON hMainIcon = ::LoadIcon(hMainMod, MAKEINTRESOURCE(resource_number));
assert (hMainIcon);
// Load kernel 32 library
HMODULE hMod = LoadLibrary( _T( "Kernel32.dll" ));
assert (hMod);
// Load console icon changing procedure
typedef DWORD (__stdcall *SCI)(HICON);
SCI pfnSetConsoleIcon = reinterpret_cast<SCI>(GetProcAddress(hMod, "SetConsoleIcon"));
assert (pfnSetConsoleIcon);
// Call function to change icon
pfnSetConsoleIcon (hMainIcon);
FreeLibrary (hMod);
}// End ChangeIcon
int GetYear()
{
time_t t = time(NULL);
tm* timePtr = localtime(&t);
return timePtr->tm_year + 1900;
}
@@ -0,0 +1,522 @@
#include "includes.h"
int TicTacToe::easyMoves[] = { 4, 0, 2, 6, 8, 1, 3, 5, 7 };
enum { NUL = 0, SOH, STX, ETX, EOT, ENQ, ACK, BEL, BS, HT,
LF, VT, FF, CR, SO, SI, DLE, DC1, DC2, DC3, DC4, NAK,
SYN, ETB, CAN, EM, SUB, ESC, FS, GS, RS, US, DEL = 127 };
TicTacToe::TicTacToe(string playername, char playerchar, char computerchar)
{
setplayername(playername);
sethuman(playerchar);
setcomputer(computerchar);
initboard();
initwins();
instructionsdisplayed = false;
}
void TicTacToe::initwins()
{
wins = vector< vector<int> >(8, vector<int>(3));
// diagonal
wins[0][0] = 0;
wins[0][1] = 4;
wins[0][2] = 8;
wins[1][0] = 2;
wins[1][1] = 4;
wins[1][2] = 6;
// down
wins[2][0] = 0;
wins[2][1] = 3;
wins[2][2] = 6;
wins[3][0] = 1;
wins[3][1] = 4;
wins[3][2] = 7;
wins[4][0] = 2;
wins[4][1] = 5;
wins[4][2] = 8;
// across
wins[5][0] = 0;
wins[5][1] = 1;
wins[5][2] = 2;
wins[6][0] = 3;
wins[6][1] = 4;
wins[6][2] = 5;
wins[7][0] = 6;
wins[7][1] = 7;
wins[7][2] = 8;
}
void TicTacToe::initboard()
{
memset(board,' ',sizeof(char)*9);
}
void TicTacToe::displayInstructions()
{
cout << "Instructions: Use your keyboard to type in coordinates to place a "
<< endl << "token on the game board. " << endl << endl;
cout << "You are "<< gethuman() << " - the protagonist in the story - you have been chosen by the high " << endl
<< "council of Tic-Toe to defeat the evil lord " << getcomputer() << ", played by the computer. " << endl;
cout << "Being the chosen one you are able to place your '" << gethuman() << "'" << endl
<< "token first, in an any open space." << endl;
cout << "The computer then places the " << getcomputer() << " token in a free space until " << endl
<< "all spaces are filled" << endl;
cout << "You and the computer continue to place tokens in free spaces until the game" << endl
<< "board is filled " << endl;
cout << "If all spaces are filled and no winner has been declared then a rematch is "
<< endl << "called and the forces of good and evil spar once again." << endl << endl;
system("pause");
system("cls");
}
void TicTacToe::run()
{
bool iseasy = true;
if (!instructionsdisplayed)
{
displayInstructions();
instructionsdisplayed = true;
}
initboard();
while (true)
{
cout << "Would you like to play in Easy mode or Hard mode ?" << endl << "[E/h]:";
char input;
input = _getch();
cout << input << endl;
if (toupper(input) == 'E' || input == LF)
{
iseasy = true;
break;
}
else if (toupper(input) == 'H')
{
iseasy = false;
break;
}
else
{
cout << "Invalid selection, please try again." << endl;
}
}
while (true)
{
displayBoard();
if (stalemate())
{
cout << "Stale mate!" << endl;
system("pause");
break;
}
else
{
setiswin(winner());
if(getiswin())
{
cout << "Sorry, the computer wins this round." << endl;
system("pause");
break;
}
getInput();
}
displayBoard();
if (stalemate())
{
cout << "Stale mate!" << endl;
system("pause");
break;
}
else
{
setiswin(winner());
if(getiswin())
{
cout << "Congratulations! A Winner is You!" << endl;
system("pause");
break;
}
think(iseasy);
}
}
}
void TicTacToe::think(bool easy)
{
if (easy)
{
for (int i = 0; i <= 8; i++)
{
if (board[easyMoves[i]] == ' ')
{
board[easyMoves[i]] = getcomputer();
break;
}
}
}
else // hard think
{
bool movemade = false;
if ( board[4] == ' ' ) // own this.
{
board[4] = getcomputer();
movemade = true;
return;
}
else
{
int count = 0;
for (int i = 0; i < 9; i++)
{
if (board[i] == getcomputer())
count++;
//cout << count;
}
bool closerlook = false;
// do we own the middle square?
if ( board[4] == getcomputer() )
{
// we have more options
closerlook = true;
}
else
{
// special case for:
// ____________
//A | X | | |
// ------------
//B | | O | |
// ------------
//C | | | O |
// ____________
// 1 2 3
if (board[4] == gethuman() && board[8] == gethuman() && board[6] == ' ')
{
board[6] = getcomputer();
movemade = true;
}
}
if (count >= 1 && !movemade)
{
for (int i = 0; i <= 7; i++) // win/block in one
{
//cout << "hard think pass: " << i << endl;
// defensive think - block before win
if (!movemade && board[wins[i][0]] == gethuman() && board[wins[i][1]] == gethuman() && board[wins[i][2]] == ' ')
{
board[wins[i][2]] = getcomputer();
movemade = true;
break;
}
if (!movemade && board[wins[i][1]] == gethuman() && board[wins[i][2]] == gethuman() && board[wins[i][0]] == ' ')
{
board[wins[i][0]] = getcomputer();
movemade = true;
break;
}
if (!movemade && board[wins[i][2]] == gethuman() && board[wins[i][0]] == gethuman() && board[wins[i][1]] == ' ')
{
board[wins[i][1]] = getcomputer();
movemade = true;
break;
}
//offensive think - seal the win if no blocks can be made?
if (!movemade && board[wins[i][0]] == getcomputer() && board[wins[i][1]] == getcomputer() && board[wins[i][2]] == ' ')
{
board[wins[i][2]] = getcomputer();
movemade = true;
break;
}
if (!movemade && board[wins[i][1]] == getcomputer() && board[wins[i][2]] == getcomputer() && board[wins[i][0]] == ' ')
{
board[wins[i][0]] = getcomputer();
movemade = true;
break;
}
if (!movemade && board[wins[i][2]] == getcomputer() && board[wins[i][0]] == getcomputer() && board[wins[i][1]] == ' ')
{
board[wins[i][1]] = getcomputer();
movemade = true;
break;
}
}
}
if (!movemade) // we still haven't made a move!? win in two? what are we doing? find first open space
{
for (int i = 0; i < 9; i++)
{
if (board[i] == ' ' )
{
board [i] = getcomputer();
movemade = true;
break;
}
}
}
}
}
}
bool TicTacToe::stalemate()
{
for (int i = 0; i < 9; i++)
{
if (board[i] == ' ')
{
return false;
}
}
return true;
}
void TicTacToe::displayBoard()
{
cout << " ____________" << endl;
cout << "A | " << board[0] << " | " << board[1] << " | " << board[2] << " |" << endl;
cout << " ------------" << endl;
cout << "B | " << board[3] << " | " << board[4] << " | " << board[5] << " |" << endl;
cout << " ------------" << endl;
cout << "C | " << board[6] << " | " << board[7] << " | " << board[8] << " |" << endl;
cout << " ____________" << endl;
cout << " 1 2 3 " << endl << endl;
}
void TicTacToe::getInput()
{
while (true)
{
cout << "Coordinates" << endl << ">:";
string input;
cin >> input;
bool isBad = false;
if (isalpha(input[0]) && isdigit(input[1]))
{
if ( toupper(input[0]) > 'C' || toupper(input[0]) < 'A' || input[1]-'0' > 3 || input[1]-'0' < 1 )
{
isBad = true;
}
else
{
switch (toupper(input[0]))
{
case 'A':
switch (input[1])
{
case '1':
if (board[0] == ' ')
{
board[0] = gethuman();
}
else
{
isBad = true;
}
break;
case '2':
if (board[1] == ' ')
{
board[1] = gethuman();
}
else
{
isBad = true;
}
break;
case '3':
if (board[2] == ' ')
{
board[2] = gethuman();
}
else
{
isBad = true;
}
break;
}
break;
case 'B':
switch (input[1])
{
case '1':
if (board[3] == ' ')
{
board[3] = gethuman();
}
else
{
isBad = true;
}
break;
case '2':
if (board[4] == ' ')
{
board[4] = gethuman();
}
else
{
isBad = true;
}
break;
case '3':
if (board[5] == ' ')
{
board[5] = gethuman();
}
else
{
isBad = true;
}
break;
}
break;
case 'C':
switch (input[1])
{
case '1':
if (board[6] == ' ')
{
board[6] = gethuman();
}
else
{
isBad = true;
}
break;
case '2':
if (board[7] == ' ')
{
board[7] = gethuman();
}
else
{
isBad = true;
}
break;
case '3':
if (board[8] == ' ')
{
board[8] = gethuman();
}
else
{
isBad = true;
}
break;
}
break;
default:
isBad = true;
}
}
if (isBad)
{
cout << "Error: bad coordinates" << endl;
}
else
{
cout << "Input accepted!" << endl;
break;
}
}
else
{
cout << "Error: bad coordinates" << endl;
}
}
}
bool TicTacToe::winner()
{
for (int i = 0; i < 2; i++)
{
char whatTest = gethuman();
if (i == 1)
whatTest = getcomputer();
for (int j = 0; j < 8; j++) // 8 ways to win
{
int thiswin[3] = { wins[j][0], wins[j][1], wins[j][2] };
// cout << board[thiswin[0]] << board[thiswin[1]] << board[thiswin[2]] << endl;
if (board[thiswin[0]] == whatTest && board[thiswin[1]] == whatTest && board[thiswin[2]] == whatTest)
return true;
}
}
return false;
}
bool TicTacToe::isTie()
{
for (int i = 0; i < boardsize; i++)
{
if (board[i] == ' ')
return true;
}
return false;
}
void TicTacToe::setplayername(string playername)
{
TicTacToe::playername = playername;
}
string TicTacToe::getplayername()
{
return TicTacToe::playername;
}
void TicTacToe::setiswin(bool isWin)
{
TicTacToe::isWin = isWin;
}
bool TicTacToe::getiswin()
{
return TicTacToe::isWin;
}
void TicTacToe::sethuman(char human)
{
TicTacToe::human = human;
}
char TicTacToe::gethuman()
{
return TicTacToe::human;
}
void TicTacToe::setcomputer(char computer)
{
TicTacToe::computer = computer;
}
char TicTacToe::getcomputer()
{
return TicTacToe::computer;
}
void TicTacToe::setboard(char *board)
{
*TicTacToe::board = *board;
}
char* TicTacToe::getboard()
{
return board;
}
void TicTacToe::setposition(int position)
{
TicTacToe::position = position;
}
int TicTacToe::getposition()
{
return TicTacToe::position;
}
@@ -0,0 +1,55 @@
#include "includes.h"
#pragma once
#define boardsize 9
class TicTacToe
{
private:
string playername;
bool isWin;
bool instructionsdisplayed;
char human;
char computer;
char board[boardsize];
int position;
/*
0 | 1 | 2
3 | 4 | 5
6 | 7 | 8
*/
vector< vector<int> > wins;
static int easyMoves[];
public:
TicTacToe(string playername, char playerchar, char computerchar);
void displayInstructions();
void run();
void displayBoard();
bool winner();
bool isTie();
void getInput();
void initboard();
bool stalemate();
void think(bool iseasy);
void setplayername(string playername);
string getplayername();
void setiswin(bool isWin);
bool getiswin();
void sethuman(char human);
char gethuman();
void setcomputer(char computer);
char getcomputer();
void setboard(char* board);
char* getboard();
void setposition(int position);
int getposition();
void initwins();
};
@@ -0,0 +1,84 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{24EA466B-50AD-4F6D-85B6-FE99AA7ADC3E}</ProjectGuid>
<RootNamespace>TicTacToe</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Main.cpp" />
<ClCompile Include="TicTacToe.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="includes.h" />
<ClInclude Include="main.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="TicTacToe.h" />
</ItemGroup>
<ItemGroup>
<Image Include="Icon.ico" />
<Image Include="Icon.png" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Resource.rc" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="TicTacToe.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="TicTacToe.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="includes.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="main.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="resource.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Image Include="Icon.ico">
<Filter>Resource Files</Filter>
</Image>
<Image Include="Icon.png">
<Filter>Resource Files</Filter>
</Image>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Resource.rc">
<Filter>Resource Files</Filter>
</ResourceCompile>
</ItemGroup>
</Project>
@@ -0,0 +1,22 @@
#pragma once
using namespace std;
#include <stdio.h>
#include <iostream>
#include <string>
#include <math.h>
#include <time.h>
#include <assert.h>
#include <vector>
#include <windows.h>
#include <tchar.h>
#include <conio.h>
#include <strsafe.h>
#include "resource.h"
#include "TicTacToe.h"
#define WIN32_LEAN_AND_MEAN
@@ -0,0 +1,13 @@
void ChangeConsoleTitle (const char* input);
void ChangeConsoleIcon (int resource_number);
void ConsoleSetup (char* input);
int GetYear();
enum { NUL = 0, SOH, STX, ETX, EOT, ENQ, ACK, BEL, BS, HT,
LF, VT, FF, CR, SO, SI, DLE, DC1, DC2, DC3, DC4, NAK,
SYN, ETB, CAN, EM, SUB, ESC, FS, GS, RS, US, DEL = 127 };