First version

This commit is contained in:
2022-04-11 17:28:18 +03:00
committed by GitHub
parent e1336093e3
commit a846fb892a
18 changed files with 1595 additions and 0 deletions
+256
View File
@@ -0,0 +1,256 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="5053" systemVersion="13C64" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="5053"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="NSApplication">
<connections>
<outlet property="delegate" destination="Voe-Tx-rLC" id="GzC-gU-4Uq"/>
</connections>
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application"/>
<customObject id="Voe-Tx-rLC" customClass="RIAppDelegate">
<connections>
<outlet property="ButtonChangeFirstMove" destination="AM8-Hm-CgG" id="4OL-m1-Cfx"/>
<outlet property="Buttons" destination="mAE-IZ-FuB" id="QYp-6X-XEj"/>
<outlet property="Stats" destination="ouk-dN-F1c" id="PA3-It-SiQ"/>
<outlet property="Text" destination="YyX-02-yoo" id="X2S-L1-i13"/>
<outlet property="window" destination="QvC-M9-y7g" id="gIp-Ho-8D9"/>
</connections>
</customObject>
<customObject id="YLy-65-1bz" customClass="NSFontManager"/>
<menu title="Main Menu" systemMenu="main" id="AYu-sK-qS6">
<items>
<menuItem title="Tic-Tac-Toe" id="1Xt-HY-uBw">
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="Tic-Tac-Toe" systemMenu="apple" id="uQy-DD-JDr">
<items>
<menuItem title="About Tic-Tac-Toe" id="5kV-Vb-QxS">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="orderFrontStandardAboutPanel:" target="-1" id="Exp-CZ-Vem"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="VOq-y0-SEH"/>
<menuItem title="Preferences…" keyEquivalent="," id="BOF-NM-1cW"/>
<menuItem isSeparatorItem="YES" id="wFC-TO-SCJ"/>
<menuItem title="Hide Tic-Tac-Toe" keyEquivalent="h" id="Olw-nP-bQN">
<connections>
<action selector="hide:" target="-1" id="PnN-Uc-m68"/>
</connections>
</menuItem>
<menuItem title="Hide Others" keyEquivalent="h" id="Vdr-fp-XzO">
<modifierMask key="keyEquivalentModifierMask" option="YES" command="YES"/>
<connections>
<action selector="hideOtherApplications:" target="-1" id="VT4-aY-XCT"/>
</connections>
</menuItem>
<menuItem title="Show All" id="Kd2-mp-pUS">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="unhideAllApplications:" target="-1" id="Dhg-Le-xox"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="kCx-OE-vgT"/>
<menuItem title="Quit Tic-Tac-Toe" keyEquivalent="q" id="4sb-4s-VLi">
<connections>
<action selector="terminate:" target="-1" id="Te7-pn-YzF"/>
</connections>
</menuItem>
</items>
</menu>
</menuItem>
<menuItem title="File" id="dMs-cI-mzQ">
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="File" id="bib-Uj-vzu">
<items>
<menuItem title="New Game" keyEquivalent="n" id="Was-JA-tGl">
<connections>
<action selector="NewGame:" target="Voe-Tx-rLC" id="wFx-tD-VhG"/>
</connections>
</menuItem>
</items>
</menu>
</menuItem>
<menuItem title="Help" id="wpr-3q-Mcd">
<modifierMask key="keyEquivalentModifierMask"/>
</menuItem>
</items>
</menu>
<window title="Tic-Tac-Toe" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" animationBehavior="default" id="QvC-M9-y7g">
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES"/>
<rect key="contentRect" x="335" y="390" width="182" height="337"/>
<rect key="screenRect" x="0.0" y="0.0" width="1440" height="900"/>
<view key="contentView" id="EiT-Mj-1SZ">
<rect key="frame" x="0.0" y="0.0" width="182" height="337"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="YyX-02-yoo">
<rect key="frame" x="18" y="149" width="146" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" alignment="center" title="Hello!" id="kDN-tP-KI6">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<connections>
<action selector="Text:" target="Voe-Tx-rLC" id="IvS-Y9-7fO"/>
</connections>
</textField>
<matrix verticalHuggingPriority="750" fixedFrame="YES" mode="highlight" allowsEmptySelection="NO" autosizesCells="NO" translatesAutoresizingMaskIntoConstraints="NO" id="mAE-IZ-FuB">
<rect key="frame" x="20" y="174" width="144" height="144"/>
<autoresizingMask key="autoresizingMask"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
<size key="cellSize" width="48" height="48"/>
<buttonCell key="prototype" type="square" bezelStyle="shadowlessSquare" imagePosition="only" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="did-IZ-byd">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<cells>
<column>
<buttonCell type="square" bezelStyle="shadowlessSquare" imagePosition="overlaps" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="HfB-y0-tE5">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system" size="25"/>
</buttonCell>
<buttonCell type="square" bezelStyle="shadowlessSquare" imagePosition="overlaps" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="EKJ-zh-clS">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system" size="25"/>
</buttonCell>
<buttonCell type="square" bezelStyle="shadowlessSquare" imagePosition="only" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="Fe1-Kh-rT6">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system" size="25"/>
</buttonCell>
</column>
<column>
<buttonCell type="square" bezelStyle="shadowlessSquare" imagePosition="only" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="Xb8-QH-2x1">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system" size="25"/>
</buttonCell>
<buttonCell type="square" bezelStyle="shadowlessSquare" imagePosition="only" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="6H1-EM-G0P">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system" size="25"/>
</buttonCell>
<buttonCell type="square" bezelStyle="shadowlessSquare" imagePosition="only" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="HOv-bu-0Hl">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system" size="25"/>
</buttonCell>
</column>
<column>
<buttonCell type="square" bezelStyle="shadowlessSquare" imagePosition="only" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="ttM-wu-qKy">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system" size="25"/>
</buttonCell>
<buttonCell type="square" bezelStyle="shadowlessSquare" imagePosition="overlaps" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="dKZ-n5-hr0">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system" size="25"/>
</buttonCell>
<buttonCell type="square" bezelStyle="shadowlessSquare" imagePosition="overlaps" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="4gq-G3-jDN">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system" size="25"/>
</buttonCell>
</column>
</cells>
<connections>
<action selector="ClickMatrix:" target="Voe-Tx-rLC" id="J1f-Yj-bQ1"/>
</connections>
</matrix>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="bIJ-gQ-UVt">
<rect key="frame" x="20" y="122" width="74" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="You play:" id="mDp-ES-xFN">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<matrix verticalHuggingPriority="750" fixedFrame="YES" mode="highlight" allowsEmptySelection="NO" autosizesCells="NO" translatesAutoresizingMaskIntoConstraints="NO" id="ouk-dN-F1c">
<rect key="frame" x="19" y="21" width="144" height="85"/>
<autoresizingMask key="autoresizingMask"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
<size key="cellSize" width="72" height="17"/>
<textFieldCell key="prototype" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="Label" id="Ylh-Hw-lPT">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<cells>
<column>
<textFieldCell scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" alignment="right" title="Wins:" id="iH5-Oh-hWe">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<textFieldCell scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" alignment="right" title="Loses:" id="4VH-Sx-NFy">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<textFieldCell scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" alignment="right" title="Draws:" id="kUx-qK-aae">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<textFieldCell scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" alignment="right" title="Win rate: " id="Mc0-LH-C6K">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<textFieldCell scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" alignment="right" title="Lose rate:" id="DJH-A4-EXI">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</column>
<column>
<textFieldCell scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="0" id="Iha-N9-vxh">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<textFieldCell scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="0" id="dQq-sD-TTJ">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<textFieldCell scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="0" id="36D-ga-V34">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<textFieldCell scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="0%" id="3PM-5R-Z2N">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<textFieldCell scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="0%" id="KNl-oE-eGY">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</column>
</cells>
</matrix>
<comboBox verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="AM8-Hm-CgG">
<rect key="frame" x="94" y="115" width="73" height="26"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<comboBoxCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" borderStyle="bezel" drawsBackground="YES" completes="NO" numberOfVisibleItems="5" id="96W-4a-8Yh">
<font key="font" metaFont="system"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
<objectValues>
<string>X</string>
<string>O</string>
<string>Dual</string>
</objectValues>
</comboBoxCell>
<connections>
<action selector="FirstMoveChange:" target="Voe-Tx-rLC" id="l1L-eJ-sQn"/>
</connections>
</comboBox>
</subviews>
</view>
</window>
</objects>
</document>
@@ -0,0 +1,58 @@
{
"images" : [
{
"idiom" : "mac",
"size" : "16x16",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "16x16",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "32x32",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "32x32",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "128x128",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "128x128",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "256x256",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "256x256",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "512x512",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "512x512",
"scale" : "2x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
+41
View File
@@ -0,0 +1,41 @@
//
// RIAppDelegate.h
// Tic-Tac-Toe
//
// Created by RUSlan on 23.03.14.
// Copyright (c) 2014 RUSlan. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface RIAppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
- (IBAction)NewGame:(id)sender;
- (IBAction)ClickMatrix:(id)sender;
- (IBAction)Checking;
- (IBAction)CheckingDraw;
- (IBAction)FirstMoveChange:(id)sender;
- (IBAction)StatsUpdate:(id)sender;
@property (weak) IBOutlet NSMatrix *Buttons;
@property (weak) IBOutlet NSTextField *Text;
@property (weak) IBOutlet NSMatrix *Stats;
@property (weak) IBOutlet NSButton *FirstMoveMenu;
@property (weak) IBOutlet NSComboBox *ButtonChangeFirstMove;
bool cellEq(id array, int firstRow, int firstColumn, int secondRow, int secondColumn);
void computerMv(id array, id Text);
void changeXO();
@end
+482
View File
@@ -0,0 +1,482 @@
//
// RIAppDelegate.m
// Tic-Tac-Toe
//
// Created by RUSlan on 23.03.14.
// Copyright (c) 2014 RUSlan. All rights reserved.
//
#import "RIAppDelegate.h"
NSString *playerMove;
NSString *computerMove;
NSString *player1Move;
NSString *player2Move;
bool gameWasEnded;
bool computerPlaysFirst;
bool firstPlayerMove;
bool twoPlayerMode;
NSInteger wins;
NSInteger loses;
NSInteger draws;
NSInteger player1Wins;
NSInteger player2Wins;
NSInteger dualDraws;
@implementation RIAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
playerMove = [NSString new];
computerMove = [NSString new];
player1Move = [NSString new];
player2Move = [NSString new];
player1Move = @"X";
player2Move = @"O";
gameWasEnded = false;
wins = 0;
loses = 0;
draws = 0;
player1Wins = 0;
player2Wins = 0;
dualDraws = 0;
[_ButtonChangeFirstMove selectItemAtIndex: 0];
[self FirstMoveChange: NULL];
[self NewGame: NULL];
}
- (IBAction)NewGame:(id)sender {
[_Text setStringValue: @"New Game"];
firstPlayerMove = true;
[_ButtonChangeFirstMove setEnabled: true];
[self StatsUpdate: NULL];
gameWasEnded = false;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
[[_Buttons cellAtRow: i column: j] setTitle: @""];
}
}
if (computerPlaysFirst) {
computerMv(_Buttons, _Text);
}
}
- (IBAction)ClickMatrix:(id)sender {
if (gameWasEnded) {
[self NewGame: NULL];
}
else {
[_Text setStringValue: @""];
[_ButtonChangeFirstMove setEnabled: false];
if ([[[sender selectedCell] title] compare: @""]) {
[_Text setStringValue: @"Illegal move!"];
}
else {
[self Checking];
if (!gameWasEnded) {
[self CheckingDraw];
}
if (twoPlayerMode) {
[[sender selectedCell] setTitle: (firstPlayerMove) ? player1Move : player2Move];
[self Checking];
if (!gameWasEnded) {
[self CheckingDraw];
}
firstPlayerMove = !firstPlayerMove;
}
else {
[[sender selectedCell] setTitle: playerMove];
[self Checking];
if (!gameWasEnded) {
[self CheckingDraw];
}
if (!gameWasEnded) {
computerMv(sender, _Text);
[self Checking];
if (!gameWasEnded) {
[self CheckingDraw];
}
}
}
}
}
}
- (IBAction)Checking {
if ( (cellEq(_Buttons, 0, 0, 1, 1) && cellEq(_Buttons, 0, 0, 2, 2)) ||
(cellEq(_Buttons, 0, 2, 1, 1) && cellEq(_Buttons, 0, 2, 2, 0)) ) {
if (twoPlayerMode) {
if ([[_Buttons cellAtRow: 1 column: 1] title] == player1Move) {
[_Text setStringValue: @"Player 1 won!"];
player1Wins++;
}
else {
[_Text setStringValue: @"Player 2 won!"];
player2Wins++;
}
}
else {
if ([[_Buttons cellAtRow: 1 column: 1] title] == playerMove) {
[_Text setStringValue: @"Player won!"];
wins++;
}
else {
[_Text setStringValue: @"Computer won!"];
loses++;
}
}
gameWasEnded = true;
}
for (int i = 0; i < 3; i++) {
if ( cellEq(_Buttons, 0, i, 1, i) && cellEq(_Buttons, 0, i, 2, i) ) {
if (twoPlayerMode) {
if ([[_Buttons cellAtRow: 0 column: i] title] == player1Move) {
[_Text setStringValue: @"Player 1 won!"];
player1Wins++;
}
else {
[_Text setStringValue: @"Player 2 won!"];
player2Wins++;
}
}
else {
if ([[_Buttons cellAtRow: 0 column: i] title] == playerMove) {
[_Text setStringValue: @"Player won!"];
wins++;
}
else {
[_Text setStringValue: @"Computer won!"];
loses++;
}
}
gameWasEnded = true;
break;
}
if ( cellEq(_Buttons, i, 0, i, 1) && cellEq(_Buttons, i, 0, i, 2) ) {
if (twoPlayerMode) {
if ([[_Buttons cellAtRow: i column: 0] title] == player1Move) {
[_Text setStringValue: @"Player 1 won!"];
player1Wins++;
}
else {
[_Text setStringValue: @"Player 2 won!"];
player2Wins++;
}
}
else {
if ([[_Buttons cellAtRow: i column: 0] title] == playerMove) {
[_Text setStringValue: @"Player won!"];
wins++;
}
else {
[_Text setStringValue: @"Computer won!"];
loses++;
}
}
gameWasEnded = true;
break;
}
}
}
- (IBAction)CheckingDraw {
bool full = true;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
full = full && ![[[_Buttons cellAtRow: i column: j] title] isEqual: @""];
}
}
if (full) {
[_Text setStringValue: @"Draw!"];
gameWasEnded = true;
if (twoPlayerMode) {
dualDraws++;
}
else {
draws++;
}
}
}
- (IBAction)FirstMoveChange:(id)sender {
switch ([_ButtonChangeFirstMove indexOfSelectedItem]) {
case 0:
computerPlaysFirst = false;
twoPlayerMode = false;
break;
case 1:
computerPlaysFirst = true;
twoPlayerMode = false;
break;
case 2:
computerPlaysFirst = false;
twoPlayerMode = true;
firstPlayerMove = true;
break;
}
changeXO();
[self NewGame: NULL];
[self StatsUpdate: NULL];
}
- (IBAction)StatsUpdate:(id)sender {
NSInteger percentW;
NSInteger percentL;
NSInteger games;
if (twoPlayerMode) {
games = wins + loses + draws;
if (games) {
percentW = round( ((double)wins)/(games) * 100 );
percentL = round( ((double)loses)/(games) * 100 );
}
else {
percentW = 0;
percentL = 0;
}
[[_Stats cellAtRow: 0 column: 0] setStringValue: @"P1 wins:"];
[[_Stats cellAtRow: 1 column: 0] setStringValue: @"P2 wins:"];
[[_Stats cellAtRow: 2 column: 0] setStringValue: @"Draws:"];
[[_Stats cellAtRow: 3 column: 0] setStringValue: @"P1 Win rate:"];
[[_Stats cellAtRow: 4 column: 0] setStringValue: @"P2 Win rate:"];
[[_Stats cellAtRow: 0 column: 1] setIntegerValue: player1Wins];
[[_Stats cellAtRow: 1 column: 1] setIntegerValue: player2Wins];
[[_Stats cellAtRow: 2 column: 1] setIntegerValue: dualDraws];
[[_Stats cellAtRow: 3 column: 1] setStringValue:
[NSString stringWithFormat: @"%ld%c", (long)percentW, '%']];
[[_Stats cellAtRow: 4 column: 1] setStringValue:
[NSString stringWithFormat: @"%ld%c", (long)percentL, '%']];
}
else {
games = wins + loses + draws;
if (games) {
percentW = round( ((double)wins)/(games) * 100 );
percentL = round( ((double)loses)/(games) * 100 );
}
else {
percentW = 0;
percentL = 0;
}
[[_Stats cellAtRow: 0 column: 0] setStringValue: @"Wins:"];
[[_Stats cellAtRow: 1 column: 0] setStringValue: @"Loses:"];
[[_Stats cellAtRow: 2 column: 0] setStringValue: @"Draws:"];
[[_Stats cellAtRow: 3 column: 0] setStringValue: @"Win rate:"];
[[_Stats cellAtRow: 4 column: 0] setStringValue: @"Lose rate:"];
[[_Stats cellAtRow: 0 column: 1] setIntegerValue: wins];
[[_Stats cellAtRow: 1 column: 1] setIntegerValue: loses];
[[_Stats cellAtRow: 2 column: 1] setIntegerValue: draws];
[[_Stats cellAtRow: 3 column: 1] setStringValue:
[NSString stringWithFormat: @"%ld%c", (long)percentW, '%']];
[[_Stats cellAtRow: 4 column: 1] setStringValue:
[NSString stringWithFormat: @"%ld%c", (long)percentL, '%']];
}
}
bool cellEq(id array, int firstRow, int firstColumn, int secondRow, int secondColumn) {
if (![[[array cellAtRow: firstRow column: firstColumn] title] isEqual: @""]) {
if ([[array cellAtRow: firstRow column: firstColumn] title] ==
[[array cellAtRow: secondRow column: secondColumn] title]) {
return true;
}
else {
return false;
}
}
else {
return false;
}
}
void computerMv(id array, id Text) {
id cellC = NULL;
id cellP = NULL;
for (int i = 0; i < 3; i++) {
if ((cellEq(array, i, 0, i, 1)) &&
([[[array cellAtRow: i column: 2] title] isEqual: @""]))
{
if ([[[array cellAtRow: i column: 0] title] isEqual: computerMove]) {
cellC = [array cellAtRow: i column: 2];
}
else {
cellP = [array cellAtRow: i column: 2];
}
break;
}
else if ((cellEq(array, i, 0, i, 2)) &&
([[[array cellAtRow: i column: 1] title] isEqual: @""]))
{
if (([[[array cellAtRow: i column: 0] title] isEqual: computerMove])) {
cellC = [array cellAtRow: i column: 1];
}
else {
cellP = [array cellAtRow: i column: 1];
}
break;
}
else if ((cellEq(array, i, 2, i, 1)) &&
([[[array cellAtRow: i column: 0] title] isEqual: @""]))
{
if ([[[array cellAtRow: i column: 1] title] isEqual: computerMove]) {
cellC = [array cellAtRow: i column: 0];
}
else {
cellP = [array cellAtRow: i column: 0];
}
break;
}
}
for (int i = 0; i < 3; i++) {
if ((cellEq(array, 0, i, 1, i)) &&
([[[array cellAtRow: 2 column: i] title] isEqual: @""]))
{
if ([[[array cellAtRow: 0 column: i] title] isEqual: computerMove]) {
cellC = [array cellAtRow: 2 column: i];
}
else {
cellP = [array cellAtRow: 2 column: i];
}
break;
}
else if ((cellEq(array, 0, i, 2, i)) &&
([[[array cellAtRow: 1 column: i] title] isEqual: @""]))
{
if ([[[array cellAtRow: 0 column: i] title] isEqual: computerMove]) {
cellC = [array cellAtRow: 1 column: i];
}
else {
cellP = [array cellAtRow: 1 column: i];
}
break;
}
else if ((cellEq(array, 2, i, 1, i)) &&
([[[array cellAtRow: 0 column: i] title] isEqual: @""]))
{
if ([[[array cellAtRow: 2 column: i] title] isEqual: computerMove]) {
cellC = [array cellAtRow: 0 column: i];
}
else {
cellP = [array cellAtRow: 0 column: i];
}
break;
}
}
if ((cellEq(array, 0, 0, 1, 1)) &&
([[[array cellAtRow: 2 column: 2] title] isEqual: @""]))
{
if ([[[array cellAtRow: 0 column: 0] title] isEqual: computerMove]) {
cellC = [array cellAtRow: 2 column: 2];
}
else {
cellP = [array cellAtRow: 2 column: 2];
}
}
else if ((cellEq(array, 0, 0, 2, 2)) &&
([[[array cellAtRow: 1 column: 1] title] isEqual: @""]))
{
if ([[[array cellAtRow: 0 column: 0] title] isEqual: computerMove]) {
cellC = [array cellAtRow: 1 column: 1];
}
else {
cellP = [array cellAtRow: 1 column: 1];
}
}
else if ((cellEq(array, 2, 2, 1, 1)) &&
([[[array cellAtRow: 0 column: 0] title] isEqual: @""]))
{
if ([[[array cellAtRow: 2 column: 2] title] isEqual: computerMove]) {
cellC = [array cellAtRow: 0 column: 0];
}
else {
cellP = [array cellAtRow: 0 column: 0];
}
}
else if ((cellEq(array, 0, 2, 1, 1)) &&
([[[array cellAtRow: 2 column: 0] title] isEqual: @""]))
{
if ([[[array cellAtRow: 0 column: 2] title] isEqual: computerMove]) {
cellC = [array cellAtRow: 2 column: 0];
}
else {
cellP = [array cellAtRow: 2 column: 0];
}
}
else if ((cellEq(array, 2, 0, 0, 2)) &&
([[[array cellAtRow: 1 column: 1] title] isEqual: @""]))
{
if ([[[array cellAtRow: 0 column: 2] title] isEqual: computerMove]) {
cellC = [array cellAtRow: 1 column: 1];
}
else {
cellP = [array cellAtRow: 1 column: 1];
}
}
else if ((cellEq(array, 2, 0, 1, 1)) &&
([[[array cellAtRow: 0 column: 2] title] isEqual: @""]))
{
if ([[[array cellAtRow: 1 column: 1] title] isEqual: computerMove]) {
cellC = [array cellAtRow: 0 column: 2];
}
else {
cellP = [array cellAtRow: 0 column: 2];
}
}
if (cellC) {
[cellC setTitle: computerMove];
}
else if (cellP) {
[cellP setTitle: computerMove];
}
else {
while (true) {
int i = random() % 3;
int j = random() % 3;
if ([[[array cellAtRow: i column: j] title] isEqualToString: @""]) {
[[array cellAtRow: i column: j] setTitle: computerMove];
return;
}
}
}
}
void changeXO()
{
playerMove = (computerPlaysFirst) ? @"O" : @"X";
computerMove = (computerPlaysFirst) ? @"X" : @"O";
}
@end
+34
View File
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>RUSlan.${PRODUCT_NAME:rfc1034identifier}</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
<string>${MACOSX_DEPLOYMENT_TARGET}</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2014 RUSlan. All rights reserved.</string>
<key>NSMainNibFile</key>
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
</dict>
</plist>
+9
View File
@@ -0,0 +1,9 @@
//
// Prefix header
//
// The contents of this file are implicitly included at the beginning of every source file.
//
#ifdef __OBJC__
#import <Cocoa/Cocoa.h>
#endif
+24
View File
@@ -0,0 +1,24 @@
{\rtf1\ansi\ansicpg1252\cocoartf1265\cocoasubrtf190
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\paperw11900\paperh16840\vieww9600\viewh8400\viewkind0
\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720
\f0\b\fs24 \cf0 UserInterface:
\b0 \
RUSlan\
\
\b Game logic:
\b0 \
RUSlan\
\
\b Bugs:
\b0 \
Gaidjin-kun\
\
\b Testers:
\b0 \
Gaidjin-kun}
+2
View File
@@ -0,0 +1,2 @@
/* Localized versions of Info.plist keys */
+14
View File
@@ -0,0 +1,14 @@
//
// main.m
// Tic-Tac-Toe
//
// Created by RUSlan on 23.03.14.
// Copyright (c) 2014 RUSlan. All rights reserved.
//
#import <Cocoa/Cocoa.h>
int main(int argc, const char * argv[])
{
return NSApplicationMain(argc, argv);
}