Khangai Robot Play - Naive  01
THe naive play of the Khangai Robot
moore.h
Go to the documentation of this file.
1 #ifndef _MOORE_H_
2 #define _MOORE_H_
3 
4 #include "state.h"
5 #include "array.h"
6 
7 template <size_t N_States, size_t N_Inputs>
9 {
10 public:
11  Moore_Machine(State* (&st)[N_States],
12  int (&in)[N_Inputs],
13  size_t (&d)[N_States][N_Inputs]) {
14 
15  arrCopy(states_, st);
16  arrCopy(inputs_, in);
17  arrCopy(del_, d);
18 
19  curr_state_ = states_[0];
20 
21  }
22  Moore_Machine() = default;
23  Moore_Machine(Moore_Machine &&) = default;
24  Moore_Machine(const Moore_Machine &) = default;
25  Moore_Machine &operator=(Moore_Machine &&) = default;
26  Moore_Machine &operator=(const Moore_Machine &) = default;
28 
29  int feed(int input) {
30  int st_index = arrIndex(states_, curr_state_);
31  if (st_index == -1) {
32  return -1;
33  }
34 
35  int in_index = arrIndex(inputs_, input);
36  if (in_index == -1) {
37  return -1;
38  }
39 
40  // The state transition table gives the index of the new state.
41  // d : S x I -> S
42  curr_state_ = states_[del_[st_index][in_index]];
43  // For a moore machine the output function is called for each state.
45 
46  return curr_state_->get_ID();
47  }
48 
49 private:
50  State* states_[N_States];
51  int inputs_[N_Inputs];
52  size_t del_[N_States][N_Inputs];
53 
55 };
56 
57 #endif // !_MOORE_H_
Definition: state.h:6
State * states_[N_States]
Definition: moore.h:50
void call_StateFunc() const
Definition: state.h:21
Moore_Machine()=default
int feed(int input)
Definition: moore.h:29
int arrIndex(T(&arr)[N], T elem)
Definition: array.h:59
~Moore_Machine()
Definition: moore.h:27
int get_ID() const
Definition: state.h:20
Moore_Machine & operator=(Moore_Machine &&)=default
void arrCopy(T(&dest)[N], const T(&src)[N])
Definition: array.h:70
size_t del_[N_States][N_Inputs]
Definition: moore.h:52
Definition: moore.h:8
int inputs_[N_Inputs]
Definition: moore.h:51
State * curr_state_
Definition: moore.h:54
Moore_Machine(State *(&st)[N_States], int(&in)[N_Inputs], size_t(&d)[N_States][N_Inputs])
Definition: moore.h:11