snp-lab-code/build/html/P05_TicTacToe/P05_TicTacToe.html

424 lines
22 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>05 - SNP: TicTacToe &#8212; SNP Labs documentation</title>
<link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../_static/alabaster.css" />
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="stylesheet" href="../_static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head><body>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="snp-tictactoe">
<h1><a class="toc-backref" href="#id1">05 - SNP: TicTacToe</a><a class="headerlink" href="#snp-tictactoe" title="Permalink to this headline"></a></h1>
<a class="reference internal image-reference" href="P05_TicTacToe/zhaw_neg_P2945.jpg"><img alt="P05_TicTacToe/zhaw_neg_P2945.jpg" class="align-right" src="P05_TicTacToe/zhaw_neg_P2945.jpg" style="width: 25.0px; height: 25.0px;" /></a>
<div class="contents topic" id="contents">
<p class="topic-title">Contents</p>
<ul class="simple">
<li><p><a class="reference internal" href="#snp-tictactoe" id="id1">05 - SNP: TicTacToe</a></p>
<ul>
<li><p><a class="reference internal" href="#ubersicht" id="id2">Übersicht</a></p></li>
<li><p><a class="reference internal" href="#lernziele" id="id3">Lernziele</a></p></li>
<li><p><a class="reference internal" href="#aufgabe-tictactoe" id="id4">Aufgabe: TicTacToe</a></p>
<ul>
<li><p><a class="reference internal" href="#test-driven-development" id="id5">Test-Driven-Development</a></p></li>
<li><p><a class="reference internal" href="#test-model-init" id="id6">3.2 test_model_init</a></p></li>
<li><p><a class="reference internal" href="#test-model-get-state-test-model-get-winner" id="id7">3.3 test_model_get_state test_model_get_winner</a></p></li>
<li><p><a class="reference internal" href="#test-model-can-move" id="id8">3.4 test_model_can_move</a></p></li>
<li><p><a class="reference internal" href="#test-model-move-test-model-get-win-line" id="id9">3.5 test_model_move test_model_get_win_line</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#bewertung" id="id10">4. Bewertung</a></p></li>
</ul>
</li>
</ul>
</div>
<section id="ubersicht">
<h2><a class="toc-backref" href="#id2">Übersicht</a><a class="headerlink" href="#ubersicht" title="Permalink to this headline"></a></h2>
<p>In diesem Praktikum erweitern Sie einen Programm Rahmen zu einem funktionierenden TicTacToe Spiel. Bei TicTacToe legen zwei Spieler abwechselnd auf einem 3x3 Brett einen Stein, bis ein Spieler mit einer horizontalen, vertikalen oder diagonalen Linie gewinnt, oder alle Felder besetzt sind.</p>
<img alt="../_images/TicTacToe.svg" src="../_images/TicTacToe.svg" /><p>In der Aufgabe implementieren Sie die fehlenden Funktionen bis alle Tests erfolgreich durchlau-fen. Die gewählte Vorgehensweise ist somit TDD Test-Driven-Development: es existieren zuerst Tests welche alle fehlschlagen. Schrittweise werden die Funktionen implementiert bis alle Tests erfolgreich durchlaufen.</p>
<p>Wenn die Tests erfolgreich durchlaufen, wird auch das Programm funktionieren und Sie können jemanden mit dem Spiel herausfordern 😉.</p>
</section>
<section id="lernziele">
<h2><a class="toc-backref" href="#id3">Lernziele</a><a class="headerlink" href="#lernziele" title="Permalink to this headline"></a></h2>
<p>In diesem Praktikum lernen Sie den Zugriff auf Arrays.</p>
<blockquote>
<div><ul class="simple">
<li><p>Sie können anhand einer Beschreibung im Code die fehlenden Funktionen implementieren wo auf Arrays zugegriffen wird.</p></li>
</ul>
</div></blockquote>
<p>Die Bewertung dieses Praktikums ist am Ende angegeben.</p>
<p>Erweitern Sie die vorgegebenen Code Gerüste, welche im git Repository snp-lab-code verfügbar sind.</p>
</section>
<section id="aufgabe-tictactoe">
<h2><a class="toc-backref" href="#id4">Aufgabe: TicTacToe</a><a class="headerlink" href="#aufgabe-tictactoe" title="Permalink to this headline"></a></h2>
<p>Das zu ergänzende Programm tic-tac-toe hat folgende Funktionalität:
#. es stellt ein 3x3 TicTacToe Spielbrett auf dem Terminal dar
#. es liest von stdin eine Ziffer 0…9 ein, wobei 0 für Programm-Terminieren, die übrigen Ziffern für die Wahl eines Feldes stehen
#. der erste Spielzug wird von Spieler A geführt, danach wechselt das Programm zwischen den Spielern A und B
# bei Gewinn oder bei vollem Brett ist das Spiel vorbei</p>
<p>Wenn die Aufgabe erfolgreich umgesetzt ist, können Sie das Spiel ausführen:
<code class="docutils literal notranslate"><span class="pre">`bash</span>
<span class="pre">bin/tic-tac-toe</span>
<span class="pre">`</span></code></p>
<p>Als Abnahme müssen die Tests unverändert ohne Fehler ausgeführt werden (<code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">test</span></code>).</p>
<p>Die Architektur des Programms folgt dem MVC Model-View-Control Paradigma. Dieses Paradigma besagt, dass die View (Eingabe und Darstellung) über Control (Vermittler) das Modell (die eigentliche Programm-Logik) steuert und darstellt. Dabei sind folgende Abhängigkeiten gegeben:</p>
<img alt="../_images/MVC_pattern.svg" src="../_images/MVC_pattern.svg" /><section id="test-driven-development">
<h3><a class="toc-backref" href="#id5">Test-Driven-Development</a><a class="headerlink" href="#test-driven-development" title="Permalink to this headline"></a></h3>
<p>Das Programm besteht aus folgenden Files:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p>Datei</p></td>
<td><p>ToDo</p></td>
</tr>
<tr class="row-even"><td><p>Makefile</p></td>
<td><ul class="simple">
<li></li>
</ul>
</td>
</tr>
<tr class="row-odd"><td><p>tests/tests.c</p></td>
<td><ul class="simple">
<li></li>
</ul>
</td>
</tr>
<tr class="row-even"><td><p>src/main.c</p></td>
<td><ul class="simple">
<li></li>
</ul>
</td>
</tr>
<tr class="row-odd"><td><p>src/view.h</p></td>
<td><ul class="simple">
<li></li>
</ul>
</td>
</tr>
<tr class="row-even"><td><p>src/view.c</p></td>
<td><ul class="simple">
<li></li>
</ul>
</td>
</tr>
<tr class="row-odd"><td><p>src/control.h</p></td>
<td><ul class="simple">
<li></li>
</ul>
</td>
</tr>
<tr class="row-even"><td><p>src/control.c</p></td>
<td><ul class="simple">
<li></li>
</ul>
</td>
</tr>
<tr class="row-odd"><td><p>src/model.h</p></td>
<td><ul class="simple">
<li></li>
</ul>
</td>
</tr>
<tr class="row-even"><td><p>src/model.c</p></td>
<td><p>siehe unten</p></td>
</tr>
</tbody>
</table>
<ol class="arabic simple">
<li><p>Führen Sie <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">test</span></code> aus:</p></li>
</ol>
<blockquote>
<div><p>Suite: lab test
Test: test_model_init
init_model:… 0/0 FAILED</p>
<blockquote>
<div><ol class="arabic simple">
<li><p>tests/tests.c:62 - CU_ASSERT_EQUAL_FATAL(instance-&gt;board[row][col],model_state_none)</p></li>
</ol>
</div></blockquote>
<dl>
<dt>Test: test_model_get_state …FAILED</dt><dd><ol class="arabic simple">
<li><p>tests/tests.c:62 - CU_ASSERT_EQUAL_FATAL(instance-&gt;board[row][col],model_state_none)</p></li>
</ol>
</dd>
<dt>Test: test_model_get_winner …FAILED</dt><dd><ol class="arabic simple">
<li><p>tests/tests.c:62 - CU_ASSERT_EQUAL_FATAL(instance-&gt;board[row][col],model_state_none)</p></li>
</ol>
</dd>
<dt>Test: test_model_can_move …FAILED</dt><dd><ol class="arabic simple">
<li><p>tests/tests.c:62 - CU_ASSERT_EQUAL_FATAL(instance-&gt;board[row][col],model_state_none)</p></li>
</ol>
</dd>
<dt>Test: test_model_move …FAILED</dt><dd><ol class="arabic simple">
<li><p>tests/tests.c:62 - CU_ASSERT_EQUAL_FATAL(instance-&gt;board[row][col],model_state_none)</p></li>
</ol>
</dd>
<dt>Test: test_model_get_win_line …FAILED</dt><dd><ol class="arabic simple">
<li><p>tests/tests.c:62 - CU_ASSERT_EQUAL_FATAL(instance-&gt;board[row][col],model_state_none)</p></li>
</ol>
</dd>
<dt>Run Summary: Type Total Ran Passed Failed Inactive</dt><dd><blockquote>
<div><dl class="simple">
<dt>suites 1 1 n/a 0 0</dt><dd><p>tests 6 6 0 6 0</p>
</dd>
</dl>
</div></blockquote>
<p>asserts 6 6 0 6 n/a</p>
</dd>
</dl>
</div></blockquote>
<p>2. Konzentrieren Sie sich auf den ersten Test der fehlschlägt. Dies ist ein Unit Test, welcher die Funktion <strong>model_init()</strong> prüft. Suchen Sie die Funktion in <strong>src/model.h</strong> und <strong>src/model.c</strong>.
Was ist die geforderte Funktionalität und wie ist sie implementiert?
Suchen Sie die darin aufgerufene <strong>model_init()</strong> Funktion und implementieren Sie diese.</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">void</span><span class="w"> </span><span class="nf">model_init</span><span class="p">(</span><span class="n">model_t</span><span class="w"> </span><span class="o">*</span><span class="n">instance</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="n">assert</span><span class="p">(</span><span class="n">instance</span><span class="p">);</span><span class="w"></span>
<span class="c1">// Instructions to the students:</span>
<span class="c1">// set all fields of the board to model_state_none</span>
<span class="c1">// BEGIN-STUDENTS-TO-ADD-CODE</span>
<span class="c1">// END-STUDENTS-TO-ADD-CODE }</span>
</pre></div>
</div>
<ol class="arabic simple" start="3">
<li><p>Führen Sie <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">test</span></code> und korrigieren Sie obige Funktion, bis der Test nicht mehr fehlschlägt.</p></li>
</ol>
</section>
<section id="test-model-init">
<h3><a class="toc-backref" href="#id6">3.2 test_model_init</a><a class="headerlink" href="#test-model-init" title="Permalink to this headline"></a></h3>
<p>Gehen Sie analog zur ersten Teilaufgabe vor:
1. Führen Sie <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">test</span></code> aus.
2. Suchen Sie die Funktion <strong>model_get_state()</strong> in <strong>model.h</strong> und <strong>model.c</strong>.
3. Implementieren Sie die intern benutzte Funktion <strong>get_state()</strong> gemäss der Anleitung im Code.</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">model_state_t</span><span class="w"> </span><span class="nf">model_get_state</span><span class="p">(</span><span class="n">model_t</span><span class="w"> </span><span class="o">*</span><span class="n">instance</span><span class="p">,</span><span class="w"> </span><span class="n">model_pos_t</span><span class="w"> </span><span class="n">pos</span><span class="p">)</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="n">assert</span><span class="p">(</span><span class="n">instance</span><span class="p">);</span><span class="w"></span>
<span class="n">assert_pos</span><span class="p">(</span><span class="n">pos</span><span class="p">);</span><span class="w"></span>
<span class="c1">// Instructions to the students:</span>
<span class="c1">// replace the stub implementation my access to the field at the given position.</span>
<span class="c1">// BEGIN-STUDENTS-TO-ADD-CODE</span>
<span class="k">return</span><span class="w"> </span><span class="n">model_state_none</span><span class="p">;</span><span class="w"> </span><span class="c1">// stub</span>
<span class="c1">// END-STUDENTS-TO-ADD-CODE</span>
<span class="p">}</span><span class="w"></span>
</pre></div>
</div>
<ol class="arabic simple" start="4">
<li><p>Führen Sie <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">test</span></code> und korrigieren Sie, bis die beiden Tests nicht mehr fehlschlagen.</p></li>
</ol>
</section>
<section id="test-model-get-state-test-model-get-winner">
<h3><a class="toc-backref" href="#id7">3.3 test_model_get_state test_model_get_winner</a><a class="headerlink" href="#test-model-get-state-test-model-get-winner" title="Permalink to this headline"></a></h3>
<p>Gehen Sie analog zur ersten Teilaufgabe vor:
1. Führen Sie <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">test</span></code> aus.
2. Suchen Sie die Funktion <strong>model_get_state()</strong> in <strong>model.h</strong> und <strong>model.c</strong>.
3. Implementieren Sie die intern benutzte Funktion <strong>get_state()</strong> gemäss der Anleitung im Code.</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">model_state_t</span><span class="w"> </span><span class="nf">model_get_state</span><span class="p">(</span><span class="n">model_t</span><span class="w"> </span><span class="o">*</span><span class="n">instance</span><span class="p">,</span><span class="w"> </span><span class="n">model_pos_t</span><span class="w"> </span><span class="n">pos</span><span class="p">)</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="n">assert</span><span class="p">(</span><span class="n">instance</span><span class="p">);</span><span class="w"></span>
<span class="n">assert_pos</span><span class="p">(</span><span class="n">pos</span><span class="p">);</span><span class="w"></span>
<span class="c1">// Instructions to the students:</span>
<span class="c1">// replace the stub implementation my access to the field at the given position.</span>
<span class="c1">// BEGIN-STUDENTS-TO-ADD-CODE</span>
<span class="k">return</span><span class="w"> </span><span class="n">model_state_none</span><span class="p">;</span><span class="w"> </span><span class="c1">// stub</span>
<span class="c1">// END-STUDENTS-TO-ADD-CODE</span>
<span class="p">}</span><span class="w"></span>
</pre></div>
</div>
</section>
<section id="test-model-can-move">
<h3><a class="toc-backref" href="#id8">3.4 test_model_can_move</a><a class="headerlink" href="#test-model-can-move" title="Permalink to this headline"></a></h3>
<p>Gehen Sie analog den obigen Teilaufgaben vor und implementieren Sie, gemäss Vorgaben im Code, die Funktion <strong>model_can_move()</strong>.</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">int</span><span class="w"> </span><span class="nf">model_can_move</span><span class="p">(</span><span class="n">model_t</span><span class="w"> </span><span class="o">*</span><span class="n">instance</span><span class="p">){</span><span class="w"></span>
<span class="n">assert</span><span class="p">(</span><span class="n">instance</span><span class="p">);</span><span class="w"></span>
<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">model_get_winner</span><span class="p">(</span><span class="n">instance</span><span class="p">)</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">model_state_none</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="c1">// Instructions to the students:</span>
<span class="c1">// scan all fields: return 1 with first field which equals model_state_none</span>
<span class="c1">// BEGIN-STUDENTS-TO-ADD-CODE</span>
<span class="c1">// END-STUDENTS-TO-ADD-CODE}</span>
<span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;}</span><span class="w"></span>
</pre></div>
</div>
</section>
<section id="test-model-move-test-model-get-win-line">
<h3><a class="toc-backref" href="#id9">3.5 test_model_move test_model_get_win_line</a><a class="headerlink" href="#test-model-move-test-model-get-win-line" title="Permalink to this headline"></a></h3>
<p>Schliesslich gehen Sie auch hier analog den obigen Teilaufgaben vor und implementieren Sie, gemäss Vorgaben im Code, die Funktion <strong>set_state()</strong>.</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/**</span>
<span class="cm">* @brief Sets the field on the board to the given state.</span>
<span class="cm">* @param instance [INOUT] The instance which holds the state.</span>
<span class="cm">* @param pos [IN] The affected field.</span>
<span class="cm">* @param state [IN] The new state of the field.</span>
<span class="cm">*/</span><span class="w"></span>
<span class="k">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">set_state</span><span class="p">(</span><span class="n">model_t</span><span class="w"> </span><span class="o">*</span><span class="n">instance</span><span class="p">,</span><span class="w"> </span><span class="n">model_pos_t</span><span class="w"> </span><span class="n">pos</span><span class="p">,</span><span class="w"> </span><span class="n">model_state_t</span><span class="w"> </span><span class="n">state</span><span class="p">)</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="n">assert_pos</span><span class="p">(</span><span class="n">pos</span><span class="p">);</span><span class="w"></span>
<span class="c1">// Instructions to the students:</span>
<span class="c1">// set the field of the board to the new state</span>
<span class="c1">// BEGIN-STUDENTS-TO-ADD-CODE</span>
<span class="c1">// END-STUDENTS-TO-ADD-CODE</span>
<span class="p">}</span><span class="w"></span>
</pre></div>
</div>
<p>Wenn die beiden obigen Teilaufgaben erfolgreich umgesetzt sind, laufen die Tests ohne Fehler durch und das Spiel kann gespielt werden.</p>
</section>
</section>
<section id="bewertung">
<h2><a class="toc-backref" href="#id10">4. Bewertung</a><a class="headerlink" href="#bewertung" title="Permalink to this headline"></a></h2>
<p>Die gegebenenfalls gestellten Theorieaufgaben und der funktionierende Programmcode müssen der Praktikumsbetreuung gezeigt werden. Die Lösungen müssen mündlich erklärt werden.</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 33%" />
<col style="width: 33%" />
<col style="width: 33%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p>Aufgabe</p></td>
<td><p>Kriterium</p></td>
<td><p>Gewicht</p></td>
</tr>
<tr class="row-even"><td><p>TicTacToe</p></td>
<td><p>Sie können das funktionierende Programm inklusive funktionierende Tests demonstrieren und erklären.</p></td>
<td></td>
</tr>
<tr class="row-odd"><td><p>TicTacToe</p></td>
<td><p>Teilaufgabe test_model_init</p></td>
<td><p>1</p></td>
</tr>
<tr class="row-even"><td><p>TicTacToe</p></td>
<td><p>Teilaufgabe test_model_get_state und test_model_get_winner</p></td>
<td><p>1</p></td>
</tr>
<tr class="row-odd"><td><p>TicTacToe</p></td>
<td><p>Teilaufgabe test_model_can_move</p></td>
<td><p>1</p></td>
</tr>
<tr class="row-even"><td><p>TicTacToe</p></td>
<td><p>Teilaufgabe test_model_move und test_model_get_win_line</p></td>
<td><p>1</p></td>
</tr>
</tbody>
</table>
<p>Version: 15.02.2022</p>
</section>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<p class="logo">
<a href="../index.html">
<img class="logo" src="../_static/logo.png" alt="Logo"/>
<h1 class="logo logo-name">SNP Labs</h1>
</a>
</p>
<h3>Navigation</h3>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../P01_Erste_Schritte_mit_C/README.html">01 - Erste Schritte mit C</a></li>
<li class="toctree-l1"><a class="reference internal" href="../P02_Funktionen_Datentyp_enum/README.html">02: Funktionen, Datentyp “enum”</a></li>
<li class="toctree-l1"><a class="reference internal" href="../P03_Bit_Operation_struct_typedef/README.html">03 - Bit Operationen, Struct, Typedef</a></li>
<li class="toctree-l1"><a class="reference internal" href="../P04_Modularisieren_von_C_Code/README.html">04 - Modularisieren von C Code</a></li>
<li class="toctree-l1"><a class="reference internal" href="README.html">05 - Arrays/Strings/TicTacToe</a></li>
<li class="toctree-l1"><a class="reference internal" href="../P06_Personen_Verwaltung_Linked_List/README.html">06 - Personen Verwaltung Linked List</a></li>
<li class="toctree-l1"><a class="reference internal" href="../P07_Prozesse_und_Threads/README.html">07 - Prozesse und Threads</a></li>
<li class="toctree-l1"><a class="reference internal" href="../P08_Sync/README.html">08 - Synchronisationsprobleme</a></li>
<li class="toctree-l1"><a class="reference internal" href="../P09_File_Operations/README.html">09 - File Operations</a></li>
<li class="toctree-l1"><a class="reference internal" href="../P10_IPC/README.html">10 - IPC</a></li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="../index.html">Documentation overview</a><ul>
</ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
&copy;2022, stsh.
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 4.3.2</a>
&amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.12</a>
|
<a href="../_sources/P05_TicTacToe/P05_TicTacToe.rst.txt"
rel="nofollow">Page source</a>
</div>
</body>
</html>